如何检查我的活动是否是屏幕中正在运行的当前活动

我使用Toast进行通知,但似乎即使它的活动不在当前屏幕中也会出现,并且其他一些活动已经启动。

我想检查这种情况,当活动不是当前活动时,我不会发送Toast通知。 但怎么办?

Solutions Collecting From Web of "如何检查我的活动是否是屏幕中正在运行的当前活动"

当您的Activity到达前台时,将调用其onResume()方法。 当另一个Activity出现在Activity之前时,将调用其onPause()方法。 所以你需要做的就是实现一个布尔值,指示你的Activity是否在前台:

 private boolean isInFront; @Override public void onResume() { super.onResume(); isInFront = true; } @Override public void onPause() { super.onPause(); isInFront = false; } 
 ArrayList runningactivities = new ArrayList(); ActivityManager activityManager = (ActivityManager)getBaseContext().getSystemService (Context.ACTIVITY_SERVICE); List 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(); } 

这样,您将知道指向的活动是否是当前可见活动。

这是我最终的isActivityVisiblefunction。

 protected boolean isActivityVisible() { if (this.mActivity != null) { Class klass = this.mActivity.getClass(); while (klass != null) { try { Field field = klass.getDeclaredField("mResumed"); field.setAccessible(true); Object obj = field.get(this.mActivity); return (Boolean)obj; } catch (NoSuchFieldException exception1) { // Log.e(TAG, exception1.toString()); } catch (IllegalAccessException exception2) { // Log.e(TAG, exception2.toString()); } klass = klass.getSuperclass(); } } return false; } 

我不喜欢自己处理这个状态,所以我已经实现了一个为我做这个的课程。

 package mypackage; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; // Mine extends AppCompatActivity - your's might need to extend Activity, depending on whether // you use the support library or not. public class StateTrackingActivity extends AppCompatActivity { public enum ActivityState { CREATED, RESUMED, STARTED, PAUSED, STOPPED, DESTROYED } private ActivityState _activityState; protected ActivityState getActivityState() { return _activityState; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); _activityState = ActivityState.CREATED; } @Override protected void onResume() { super.onResume(); _activityState = ActivityState.RESUMED; } @Override protected void onStart() { super.onStart(); _activityState = ActivityState.STARTED; } @Override protected void onPause() { super.onPause(); _activityState = ActivityState.PAUSED; } @Override protected void onStop() { super.onStop(); _activityState = ActivityState.STOPPED; } @Override protected void onDestroy() { super.onDestroy(); _activityState = ActivityState.DESTROYED; } } 

然后你的活动可以扩展这个,你可以通过调用getActivityState()来获得状态。

有Activity#isTaskRoot()方法

 if (BaseActivity.this instanceof Faq) { Toast.makeText(BaseActivity.this, "You are in the Same Page", Toast.LENGTH_SHORT).show(); }else { Intent intent = new Intent(BaseActivity.this, Faq.class); startActivity(intent); drawer.closeDrawer(GravityCompat.START); } 

////这是我的所有活动都在名为BaseActivity的Activity上进行扩展

  if ( getActivity() instanceof ManageCardActivity){ // your code }