简体   繁体   English

Android Activity生命周期问题

[英]Android Activity life cycle issues

What I actually Wanted ? 我真正想要的是什么?

In my app I have 4/5 activities and one background thread which talks with the server. 在我的应用程序中,我有4/5活动和一个与服务器通信的后台线程。 I wanted to have some way to get the top most activity of my app. 我想通过某种方式获得我应用的最顶层活动。 But surprisingly their was no way I could get that ? 但令人惊讶的是他们不可能得到那个?

What I did ? 我做了什么 ?

After searching for a while, I implemented a variable mCurrentOnTopActivity of Type Activity and on onResume() and onPause() of every activity I set that variable. 在搜索了一段时间之后,我实现了Type Activity的变量mCurrentOnTopActivity ,以及我设置该变量的每个活动的onResume()onPause() (That is actually a bad way). (这实际上是一种糟糕的方式)。

Where I reached ? 我到达的地方?

Now their are cases in which after receiving some messages from the server I want to do some thing on UI thread ie show some dialogs or refresh ListView , now their is no way to check whether the mCurrentOnTopActivity refers an activiy which is actually running or not (finished or out Of focus for whatever reason). 现在他们的情况是,在收到来自服务器的一些消息之后,我想在UI线程上做一些事情,即显示一些对话框或刷新ListView ,现在他们无法检查mCurrentOnTopActivity是否引用了实际运行的活动(因任何原因完成或失焦)。

What I want now ? 我现在想要什么? Now I basically want help in following two problems:- 现在我基本上想要帮助解决两个问题: -

  1. How do I know which is the currently running top most activity of my App ? 我怎么知道我的应用程序当前运行的最活跃的是哪个?
  2. How do I know whether activity is still running or not (ie I want some thing like isActicitAlive() (imaginary function) which returns true between calls of onResume() and onPause() ? 我如何知道活动是否仍在运行(即我想要一些像isActicitAlive() (imaginary function) ,它在onResume()onPause()调用之间返回true?

I implemented 2 point using a variable in activity and setting it in onResume() and onPause() . 我在活动中使用变量并在onResume()onPause()设置了2点。 But, is this the only way to do this ? 但是,这是唯一的方法吗?

ArrayList<String> runningactivities = new ArrayList<String>();

ActivityManager activityManager = (ActivityManager)getBaseContext().getSystemService (Context.ACTIVITY_SERVICE); 

List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE); 

for (int i1 = 0; i1 < services.size(); i1++) { 
    runningactivities.add(0,services.get(i1).topActivity.toString());  
} 

if(runningactivities.contains("ComponentInfo{com.app/com.app.main.MyActivity}")==true){
    Toast.makeText(getBaseContext(),"Activity is in foreground, active",1000).show(); 
}

With this, you can check, whether the activity you are looking for is running or not. 有了这个,您可以检查您正在寻找的活动是否正在运行。

esle, if you want to use the onStart() and onStop() methods, you can use the following code, esle,如果要使用onStart()onStop()方法,可以使用以下代码,

class MyActivity extends Activity {
     static boolean active = false;

      @Override
      public void onStart() {
         super.onStart();
         active = true;
      } 

      @Override
      public void onStop() {
         super.onStop();
         active = false;
      }
}

您可以使用服务绑定,以便只有最顶层的活动才能绑定到服务。

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

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