0

Having real issue understanding how to sort my issue out.

On the Home screen I have 2 buttons

When the user clicks the first button it starts a new Activity. What I am looking for is if the user clicks back the app returns to the home screen. If the user clicks the first button again it starts a new activity.

If the user clicks the second button it returns to the activity that was last started by clicking button 1

What I am having issue with is how to save the state of the activity when the user clicks back Also how to call that activity when the second button is pressed

Thanks for your Time

UPDATE

I have gone down part of this but still having issues. If I put some of the code I am using perhaps someone can point where I gone wrong.

Code for calling the new activity from main menu

Intent intent = new Intent(MainMenu.this, NewClass.class);
        intent.putExtra("value1", value1);  
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

Within the new class I have added the following :

    @Override
public void onBackPressed() {
    //super.onBackPressed();
    Intent intent = new Intent(RoundScoring.this, MainMenu.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(intent);
    Toast.makeText(this, "Back Button Pressed", Toast.LENGTH_LONG).show();
}

I do not have either a onrestoreinstancestate or onresumne in this class. only a oncreate. Do I have to add something like this to bring back the instance

On the second button on the main menu I have added this

Intent intentContiune = new Intent(MainMenu.this, NewClass.class);
            intentContiune.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intentContiune);

Thanks

James Dudley
  • 915
  • 4
  • 15
  • 35

2 Answers2

1

Try this:

Home Screen Left Button: Open the new activity with an intent flag, FLAG_ACTIVITY_NEW_TASK

Activity: Override onBackClick() on the started activity to call the home screen with an Intent instead of finishing it. Use the flag FLAG_ACTIVITY_REORDER_TO_FRONT Save activity state overriding OnSaveInstanceState

Home Screen Right Button: Call the activity with the flag FLAG_ACTIVITY_SINGLE_TOP

More info about flags: http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/

Fernando Gallego
  • 4,064
  • 31
  • 50
  • Whats the code to call the intent again using the flat FLAG_ACTIVITY_SINGLE_TOP. Thanks – James Dudley Aug 11 '11 at 13:17
  • When button 1 is pressed I am passing a couple of varibales along with it that are used in the class. Clicking the second button it does not seem to have saved these variables. Do I have do something more when OnSaveInstanceState – James Dudley Aug 11 '11 at 14:41
  • updated with some code, am I going in the right direction. Thansk – James Dudley Aug 12 '11 at 08:25
  • You have to save something to the outState variable on the onSaveInstanceState() Then have to restore the saved data in the onCreate(Bundle savedInstanceState) or override onRestoreInstanceState(Bundle savedInstanceState) – Fernando Gallego Aug 12 '11 at 08:45
  • As you can see I am pulling a value over when I start it. On this new activity I am creating an arraylist. OnSaveInstanceState() would need to save both the value and the arraylist. Correct? but in the OnCreate I am reading the value but for the first start the arraylist will not exist, how do I get that to be ingored until it is created. Thanks – James Dudley Aug 12 '11 at 08:51
  • During the on create I am using the value to create an arraylist. Issue I have is when I click the second button it breaks as the value is blank and not been pulled back yet. Cannot seem to get the onRestoreInstanceState to fire – James Dudley Aug 12 '11 at 09:30
  • Check if the value exists in the bundle and is not null before using it. – Fernando Gallego Aug 14 '11 at 16:27
0

One solution may involve passing bundles around that include the state of your activity. Using startActivityForResult(), you can return a bundle with the activity's state. When the user clicks your second button, pass in that bundle and have the activity set itself up with respect to the contents of the bundle. If the bundle doesn't contain the information you're looking for, then use the default values as if you were just starting it.

For more information:

Android: Capturing the return of an activity

Community
  • 1
  • 1
DeeV
  • 35,865
  • 9
  • 108
  • 95