Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android reset activity component state on navigation back

I have 2 activities in my application. In activity1, I set some configuration and I modify the state of some component (ex: set button state disabled). And I navigate to activity2.

In activity2 I do some stuff and at end, I finish this activity and come back to activity1.

But In activity1, my components state are the same when I leave this activity.

How to reset the components state with valuesdeclared in XML file (layout) ?

like image 583
Aure77 Avatar asked Oct 18 '25 12:10

Aure77


2 Answers

You have three callbacks in this instance available to you to manage the state of the Activity.

Since you are returning to the Activity, onRestart() will be called, and this callback is specific to this instance (it is not called the first the time Activity is created and displayed).

The other callbacks you will receive are onStart() and onResume(), which are also called any time that Activity comes to the foreground (include the first time). More on the Activity lifecycle here.

You may reset the state of your UI in any one of these places that best suits your application. There is no direct "reset" method you can call on your views to set them back to the XML defined stage, though. You will need to manually call the methods to set the view flags back to the state you require.

HTH

like image 198
devunwired Avatar answered Oct 22 '25 05:10

devunwired


When you return to the previous activity as the result of calling finish() the onActivityResult method is called. You can repopulate your view to the desired state within this method.

For example:


    ...
        Intent result_intent = new Intent();
        setResult( Activity.RESULT_OK, result_intent );
        finish();
    }

    public void onActivityResult( int requestCode, int resultCode, Intent data )
    {
        setContentView( R.layout.my_activity );
    }
like image 32
Daniel Bradley Avatar answered Oct 22 '25 04:10

Daniel Bradley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!