简体   繁体   English

如果该活动被Android运行时杀死,该如何转到上一个活动?

[英]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. 我的应用程序有问题,即当我移至上一个窗口时,控件转到我的应用程序的启动屏幕,因为前一个活动被Android运行时终止。

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 当您进行Splash活动时,我认为正确的方法是关闭并启动主要活动

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() ? 您在打电话给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. Android会自行处理。

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. 例如,如果您想以4-2-3-1之类的特定方式遍历,那么Android不允许您这样做,因此只需创建一个构造函数并为其分配当前活动,然后随时使用该变量关闭屏幕。

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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