smoke
smoke

Reputation: 361

proper android activity flow?

I am trying to wrap my head around what proper activity flow convention is.

I currently have:

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        //do stuff
        //clicklisteners setup etc
        Intent intent = new Intent(this, ExampleActivity.class);
        //putExtras
        startActivity(intent);
    }
}  

public class ExampleActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        //getExtras
        //objectA state lives here
        //do stuff
    }
}

If the user presses back when on the ExampleActivity view, and then clicks another listener that takes them to ExampleActivity, I want to be able to access "objectA" state again. How do I implement this? I am not understanding onResume or onRestart...

are these the methods to call? or is there a better convention to navigate the app activities?

Upvotes: 1

Views: 84

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317392

Android has a mechanism for having an activity pass results back to the prior activity that started it. The documentation for that is here.

Basically, you use startActivityForResult to start the second activity, the second activity uses setResult to set results, and the first activity receives those results in the onActivityResult callback when the second activity finishes.

Upvotes: 1

Amir Horev
Amir Horev

Reputation: 287

If the user presses back when on the ExampleActivity view, the ExampleActivity is dead and user is back in the MainActivity, which calls "onResume". When your are back from activity1 to activity2, activity2's onResume method is called. With that being said, after the user closed ExampleActivity objectA is destroyed.

Upvotes: 0

Related Questions