search

Home  >  Q&A  >  body text

android - 安卓如何随意控制两种界面的切换?

app的Activity分为两类A和B,共两个入口,app会定期接收网络消息,根据接收到的消息切换界面。
可能在A的页面时需要切换到B的入口,在B的页面时需要切换到A的入口。应该怎么实现?包括界面切换和activity的销毁。

我目前的想法让两类Activity分别继承不同的BaseActivity,然后在Application中保存网络消息的状态,同时保存当前显示的Activity的引用,状态改变的时候根据当前Activity的类型判断是否要做界面切换。
但具体怎么控制Activity的销毁,我不是很清楚。
我的想法是保存当前所有创建了的Activity,在切换页面后,只保留切换后的Activity,销毁其余Activity。

大家讲道理大家讲道理2892 days ago673

reply all(3)I'll reply

  • 迷茫

    迷茫2017-04-18 09:20:55

    This problem has been better solved. All activities are started in standard mode, so that all activities will be in a task. The current activity must be saved with a weak reference, and then when switching to a new activity, the finishAffinity of the current activity is called. method will end all currently running activities and then open a new activity. The code for switching activities is best written in Application. Regarding the afficity of Activity, you can read this article http://www.androidchina.net/2...

      MyActivityManager.getInstance().getCurrentActivity().finishAffinity();
              context.startActivity(new Intent(context, AdminLoginActivity.class)
                  .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:20:55

    I don’t quite understand your needs. Do you want to control the start and end of Activity through the server?

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:20:55

    You just need to write a baseActivity. For example, if you have two types of activities now, you can write a boolean abstract method in baseActivity. If it returns true, it is considered a type A activity, and false is a type B activity. Then you write An activity management class is then encapsulated into baseActivity. Each time an activity is started, it is added to a list collection and the type is saved. Then when you want to switch types, you can directly start another type of activity. If you want to destroy the previous one, just delete the other type of activity in the stack directly through the activity management class.

    reply
    0
  • Cancelreply