我用 Toast 来发出通知,但它似乎会出现,即使它的活动不在当前屏幕中并且已经启动了一些其他活动。
我想检查这种情况,当活动不是当前活动时,我不会发送 Toast 通知。但是怎么办?
我用 Toast 来发出通知,但它似乎会出现,即使它的活动不在当前屏幕中并且已经启动了一些其他活动。
我想检查这种情况,当活动不是当前活动时,我不会发送 Toast 通知。但是怎么办?
当您的 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<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();
}
这样,您将知道指向的活动是否是当前可见活动。
我不喜欢自己处理状态,所以我实现了一个为我做这件事的类。
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()
。
这是我的终极 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;
}
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 的活动上扩展
if ( getActivity() instanceof ManageCardActivity){
// your code
}