简体   繁体   中英

How to go to previous activity if the activity is killed by Android runtime?

I had a problem with an app, ie, when I am moving to the previous window the control goes to the splash screen of my app because the previous activity is killed by the Android runtime.

How do I keep the previous activity alive, or how can I create the activity again and access it from the present activity.

When you have a Splash activity, I think the correct way is closing in and starting the main activity

Intent mainIntent =new Intent(Splash.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();

Then, from your main activity start calling your subactivities.

Why has it killed your main activity? Are you calling finish() ?

You can make use of a default constructor to close and open the screen.

If you want to move between screens:

1-2-3-4

  &&

4-3-2-1 

Android itself will take care of it.

For example, if you want traverse in a specific way like 4-2-3-1 Android doesn't allow you, so simply create a constructor and assign present activity to it and make use of that variable to close the screen at any time.

For example, here is the activity class

public class One extends Activity 
{

    private One Screen;

    public One()
    {
        Screen=this;
    }

}

When you want close this activity simply use:

Screen.finish();

instead of

this.finish();

When you want invoke new activity of your liking, simply use

Screen.finish();
Intent i = new Intent(One.this, YourActivity.class);
Log.i(TAG, "calling YourActivity Screen");
startActivity(i);

If you want pass any data between the screens, you can make use of

i.putExtra("valuename", actualvalue);

and you can retrive the value using

Intent startIntent = getIntent();
String actualvalue = startIntent.getStringExtra("valuename");

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