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

时间:2010-07-16 05:27:24

标签: android android-activity screen lifecycle

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

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

7 个答案:

答案 0 :(得分:82)

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

private boolean isInFront;

@Override
public void onResume() {
    super.onResume();
    isInFront = true;
}

@Override
public void onPause() {
    super.onPause();
    isInFront = false;
}

答案 1 :(得分:7)

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(); 
}

通过这种方式,您将知道指向的活动是否是当前可见的活动。

答案 2 :(得分:2)

这是我最终的isActivityVisible功能。

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;
}

答案 3 :(得分:2)

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

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()来获取状态。

答案 4 :(得分:1)

答案 5 :(得分:1)

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的活动

上进行扩展

答案 6 :(得分:-3)

  if ( getActivity() instanceof ManageCardActivity){

   // your code
  }