简体   繁体   中英

Returning to the Home Screen and then returning to last activity

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

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM