简体   繁体   English

简历上的Android启动活动

[英]Android Launch Activity On Resume

In my application, I have 5 Activities. 在我的应用程序中,我有5个活动。 A , B , C , D , E . ABCDE Activities B , C , D , E extend Activity A which extends AppCompat. 活动BCDE扩展了活动A ,该活动A扩展了AppCompat。 When the application starts from Android Studio, Activity A launches Activity B . 当应用程序从Android Studio启动时,活动A启动活动B From Activity B , the user can decide which activity to load either C , D , or E . 用户可以从活动B中决定要加载CDE My code works fine until the user exits the application and decides to launch it again. 我的代码可以正常工作,直到用户退出应用程序并决定再次启动它为止。 The application shows Activity A and does not take the user to Activity B . 该应用程序显示活动A ,而不会将用户带到活动B I want the user to see Activity B . 我希望用户看到活动B How can I fix this? 我怎样才能解决这个问题?

Have you tried overriding the onResume() method to show activity B? 您是否尝试覆盖onResume()方法以显示活动B?

@Override
public void onResume(){
    super.onResume();
    setContentView(R.id.layout_B);
}

It sounds like you should define Activity B as your launcher in the Manifest, I am assuming Activity A was defined as the launcher by a template/default. 听起来您应该在清单中将活动B定义为启动器,我假设模板/默认值将活动A定义为启动器。

<activity name="{Activity B's name}">     
  <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
</activity>

Activity B will be the 'root' of your application, at the moment Activity B is added on-top of Activity A in the stack. 活动B将成为您应用程序的“根”,此刻将活动B添加到堆栈中活动A的顶部。

If Activity A is also doing a bunch of bootstrapping, another approach will be needed but more information is needed. 如果活动A也在进行大量引导,则将需要另一种方法,但需要更多信息。

If you want to retain application state after your app closes, you may want to use SharedPreferences : 如果要在应用程序关闭后保留应用程序状态,则可能要使用SharedPreferences

final String CURR_ACTIVITY_KEY = "curr_activity";
int currentActivityKey = 1;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
prefs.edit().putInt(CURR_ACTIVITY_KEY, currentActivityKey).commit();

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

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