Android Refresh activity when returns to it

后端 未结 7 1949
Happy的楠姐
Happy的楠姐 2021-02-07 10:41

I need a little help with refreshing one of my activities in my application. I\'m using tab host activity and connecting to web service and downloading some data from one of my

相关标签:
7条回答
  • 2021-02-07 11:18

    Override the

    onRestart()

    method so that it is called automatically when you return back from your desired task.

    Put the following code in your onRestart() method :

    @Override
    protected void onRestart() {
        super.onRestart();
        finish();
        overridePendingTransition(0, 0);
        startActivity(getIntent());
        overridePendingTransition(0, 0);
    }
    
    0 讨论(0)
  • 2021-02-07 11:19

    Call child activity using startActivityForResult with request code,SetResult from the child Activity. And when the child activity is finished, you can update you parent activity in onActivityResult method

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
    
          //Check the result and request code here and update ur activity class
    
        }
    

    Here is an example http://rahulonblog.blogspot.com/2010/05/android-startactivityforresult-example.html

    0 讨论(0)
  • 2021-02-07 11:20

    Another tricky way to do this is just start your activity on onRestart()

    @Override
    public void onRestart(){
        super.onRestart();
        Intent previewMessage = new Intent(StampiiStore.this, StampiiStore.class);
        TabGroupActivity parentActivity = (TabGroupActivity)getParent();
        parentActivity.startChildActivity("StampiiStore", previewMessage);
        this.finish();
    }
    

    That should do the trick actually. (In this code I'm showing the way it's done when you are using a custom TabActivity manager.)

    0 讨论(0)
  • 2021-02-07 11:24

    If you start your second activity with the method startActivityForResult, when you return to the first activity, onActivityResult of the first activity will be called.

    If you override it, you can refresh your activity from there.

    See more information here and here

    0 讨论(0)
  • 2021-02-07 11:25

    Override onRestart() method inside activity you want to refresh, and recreate the activity

    @Override
    protected void onRestart() {
        super.onRestart();
        this.recreate();
    }
    
    0 讨论(0)
  • 2021-02-07 11:26

    on button press:

    Intent intent = new Intent(this, SyncActivity.class);
            //intent.putExtra("someData", "Here is some data");
            startActivityForResult(intent, 1);
    

    Then in the same Activity class:

       @Override
         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if(resultCode==RESULT_OK){
             Intent refresh = new Intent(this, InitialActivity.class);
             startActivity(refresh);
             this.finish();
          }
         }
    

    The Sync Activity would have:

    setResult(RESULT_OK, null);
    finish();
    
    0 讨论(0)
提交回复
热议问题