How to check if my activity is the current activity running in the screen

扶醉桌前 提交于 2019-11-27 19:05:48
Andy Zhang

When your Activity comes to the foreground, its onResume() method will be invoked. When another Activity comes in front of your Activity, its onPause() method will be invoked. So all you need to do is implement a boolean indicating if your Activity is in the foreground:

private boolean isInFront;

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

@Override
public void onPause() {
    super.onPause();
    isInFront = false;
}
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(); 
}

This way you will know if the pointed activity is the current visible activity.

I prefer not to handle the state by myself, so I have implemented a class that does this for me.

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

Then your activity can extend this one and you can get the state by calling getActivityState().

This is my ultimate isActivityVisible function.

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

There is Activity#isTaskRoot() method

Nishanth S Babu
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);
            }

//// here am All my activities are extending on Activity called BaseActivity

amit
  if ( getActivity() instanceof ManageCardActivity){

   // your code
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!