简体   繁体   English

Android:launchMode = SingleTask问题

[英]Android: launchMode=SingleTask problem

I have an app that circles around the main activity (a main menu). 我有一个围绕主要活动(主菜单)的应用程序。 In each other app there is an option menu item that directs to this activity. 在每个其他应用程序中,有一个选项菜单项,指向此活动。

At first, I always started a new main activity when this item was selected. 首先,我总是在选择此项目时启动一个新的主要活动。 Using the intent bundle, I did tell the main activity that some initializations I do on a fresh start were not necessary. 使用intent bundle,我确实告诉了主要的活动,我在一个新的开始时做的一些初始化是没有必要的。

However, I didn't quite like the overall behavior. 但是,我不太喜欢整体行为。 I stumbled upon android:launchMode="SingleTask" and this seemed to help: now I don't recreate my main menu activity all the time; 我偶然发现了android:launchMode="SingleTask" ,这似乎有所帮助:现在我不会一直重建我的主菜单活动; also, if I press the "back" button I come back straight to the home screen. 另外,如果我按下“后退”按钮,我会直接回到主屏幕。 This feels quite nicely like a proper "main" menu. 这感觉非常像一个适当的“主菜单”。

My problem now is this: if I run another activity of my app, press home button and then reopen my app (eg using "last apps"), then I don't go back to the last activity, but to the main one. 我现在的问题是:如果我运行我的应用程序的另一个活动,按主页按钮,然后重新打开我的应用程序(例如使用“最后的应用程序”),然后我不会回到上一个活动,而是回到主要活动。 The other activity is destroyed. 其他活动被销毁。

Any ideas how I can implement the behavior of SingleTask without only being able to return to one activity? 任何想法如何实现SingleTask的行为,而不仅能够返回到一个活动?

If your other activities are declared normally with activity defaults in Android, then going back to your app should take you to the same activity where you left off (using the hardware home button) 如果你的其他活动在Android中正常宣布活动默认值,那么回到你的应用程序应该带你到你离开的同一个活动(使用硬件主页按钮)

However remember that the Android system kills applications when it requires system resources. 但请记住,Android系统在需要系统资源时会杀死应用程序。 So your app may have been killed when you went to the other application. 因此,当您转到其他应用程序时,您的应用程序可能已被杀死。 Then when you get back to your app, the default launcher activity will be restarted, which is your Menu activity. 然后,当您返回应用时,将重新启动默认启动器活动,这是您的菜单活动。

To get back to the main activity from any activity, do this: 要从任何活动返回主要活动,请执行以下操作:

public static void goHome(Context context) {
        final Intent intent = new Intent(context, HomeActivity.class); //give name of your main activity class here
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

That will clear the activity stack and get you back to your main activity. 这将清除活动堆栈并让您回到主要活动。 As you declared singleTop, it will bring the existing main activity to the foreground. 当您声明singleTop时,它会将现有的主要活动带到前台。 The flag Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all activities in the stack on top of the main activity. 标志Intent.FLAG_ACTIVITY_CLEAR_TOP将删除主活动之上的堆栈中的所有活动。 (I am assuming you are within the same application). (我假设你在同一个申请中)。

Now, all your other activities only need to include a button whose click listener invokes the method goHome(); 现在,所有其他活动只需要包含一个按钮,其click侦听器调用方法goHome();

From your main activity, if you press the hardware back button, it should exit your app. 从您的主要活动中,如果您按下硬件后退按钮,它应退出您的应用程序。

Why not call finish() on the activities that were created by the main activity? 为什么不对主要活动创建的活动调用finish()? This way you return to the main activity, without creating a new one... 这样您就可以返回主活动,而无需创建新活动...

I think you should save the state of you activity before starting another activity, and then resume your activity whenever you come back on last activity. 我认为您应该在开始另一项活动之前保存您的活动状态,然后在您返回上一次活动时恢复您的活动。 see Activity Life cycle from Android http://developer.android.com/guide/topics/fundamentals/activities.html 请参阅Android http://developer.android.com/guide/topics/fundamentals/activities.html中的活动生命周期

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

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