Sometimes, you need to get an instance of the activity currently at the top of the stack. Here are two methods and their respective scenarios. Finally, demo test data are available for reference.
The first method: Get an instance of stack top activity
Application scenario: In the project, activity inherits from a BaseActivity, in which a broadcast receiver receives a broadcast and a dialog pops up. Now open multiple activities, at this time, through three-way push and other methods, send a broadcast, base received. Because only the activity pops up on the top of the stack, not every activity pops up, we need to get an instance of the activity on the top of the stack (that is, the context, the construction method passed to dialog), instead of simply passing this, otherwise, every activity will have dialog pop-up.
Acquisition method:
In application:
public class MyApplication extends Application {
private static MyApplication mInstance;
private Activity app_activity = null;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
initGlobeActivity();
}
private void initGlobeActivity() {
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
app_activity = activity;
Log.e("onActivityCreated===", app_activity + "");
}
@Override
public void onActivityDestroyed(Activity activity) {
app_activity = activity;
Log.e("onActivityDestroyed===", app_activity + "");
}
/** Unused implementation **/
@Override
public void onActivityStarted(Activity activity) {
app_activity = activity;
Log.e("onActivityStarted===", app_activity + "");
}
@Override
public void onActivityResumed(Activity activity) {
app_activity = activity;
Log.e("onActivityResumed===", app_activity + "");
}
@Override
public void onActivityPaused(Activity activity) {
app_activity = activity;
Log.e("onActivityPaused===", app_activity + "");
}
@Override
public void onActivityStopped(Activity activity) {
app_activity = activity;
Log.e("onActivityStopped===", app_activity + "");
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
});
}
/**
* Get examples
* @return
*/
public static MyApplication getInstance() {
return mInstance;
}
/**
* Open method, externally accessible to the current top-level activity through MyApplication.getInstance().getCurrentActivity()
*/
public Activity getCurrentActivity() {
return app_activity;
}
}
Note: initGlobeActivity(), getInstance(), getCurrentActivity() are three methods
Usage method:
In the display dialog method of base:
Activity currentActivity = MyApplication.getInstance().getCurrentActivity();
Dialog dialog = new Dialog(currentActivity, R.style.PushDialog);
————————————————————————————————–
The second method: Get the full package name of the top activity on the stack
Application scenario: Just to determine if the current activity is on the top of the stack, to decide whether to play toast, etc.?
Note: Only the full package name is acquired. To judge whether the current activity is not on the top of the stack, we need to use the contains method of String method and so on. Attention should be paid at this time to avoid mistakes. For example, the top activity of the stack is called AB, which contains A. It is wrong to think that A is on the top of the stack. Best of all, the name of the activity is very special, and there is no overlap with other parts.
Create methods in the tool class:
/**
* Get the top-level Activity on the stack
*
* @param context
* @return
*/
public static String getTopActivity(Context context) {
android.app.ActivityManager manager = (android.app.ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
if (runningTaskInfos != null) {
return (runningTaskInfos.get(0).topActivity).toString();
} else
return null;
}
Use:
String topActivity_1 = Util.getTopActivity(this);
Log.e("topActivity_1", topActivity_1);
Test data:
Three existing activities: MainActivity_1, MainActivity_2, MainActivity_3
The first has a button, Click to jump to the second, the second has a button, and click to jump to the third.
Code:
package com.chen.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
/**
* Basic Activity
*/
public abstract class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Do not display title
requestWindowFeature(Window.FEATURE_NO_TITLE);
initview();
}
abstract void initview();
}
package com.chen.demo;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity_1 extends BaseActivity {
TextView tv;
@Override
void initview() {
setContentView(R.layout.activity_main_1);
String topActivity_1 = Util.getTopActivity(this);
Log.e("topActivity_1", topActivity_1);
tv = (TextView) findViewById(R.id.tv);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity_1.this.startActivity(new Intent(MainActivity_1.this, MainActivity_2.class));
}
});
}
}
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity_2 extends BaseActivity {
TextView tv_2;
@Override
void initview() {
setContentView(R.layout.activity_main_2);
String topActivity_2 = Util.getTopActivity(this);
Log.e("topActivity_2", topActivity_2);
tv_2 = (TextView) findViewById(R.id.tv_2);
tv_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity_2.this.startActivity(new Intent(MainActivity_2.this, MainActivity_3.class));
}
});
}
}
package com.chen.demo;
import android.util.Log;
public class MainActivity_3 extends BaseActivity {
@Override
void initview() {
setContentView(R.layout.activity_main_3);
String topActivity_3 = Util.getTopActivity(this);
Log.e("topActivity_3", topActivity_3);
}
}
Journal:
Program just started 02-24 10:56:12.997 13043-13043/com.chen.demo E/onActivityCreated===: com.chen.demo.MainActivity_1@eff3d78 02-24 10:56:13.030 13043-13043/com.chen.demo E/topActivity_1: ComponentInfo{com.chen.demo/com.chen.demo.MainActivity_1} 02-24 10:56:13.032 13043-13043/com.chen.demo E/onActivityStarted===: com.chen.demo.MainActivity_1@eff3d78 02-24 10:56:13.032 13043-13043/com.chen.demo E/onActivityResumed===: com.chen.demo.MainActivity_1@eff3d78 Click the button in MainActivity_1 to go to MainActivity_2 02-24 10:56:47.692 13043-13043/com.chen.demo E/onActivityPaused===: com.chen.demo.MainActivity_1@eff3d78 02-24 10:56:47.718 13043-13043/com.chen.demo E/onActivityCreated===: com.chen.demo.MainActivity_2@53c82a7 02-24 10:56:47.722 13043-13043/com.chen.demo E/topActivity_2: ComponentInfo{com.chen.demo/com.chen.demo.MainActivity_2} 02-24 10:56:47.724 13043-13043/com.chen.demo E/onActivityStarted===: com.chen.demo.MainActivity_2@53c82a7 02-24 10:56:47.724 13043-13043/com.chen.demo E/onActivityResumed===: com.chen.demo.MainActivity_2@53c82a7 02-24 10:56:48.099 13043-13043/com.chen.demo E/onActivityStopped===: com.chen.demo.MainActivity_1@eff3d78 Click the button in MainActivity_2 to go to MainActivity_3 02-24 10:57:21.984 13043-13043/com.chen.demo E/onActivityPaused===: com.chen.demo.MainActivity_2@53c82a7 02-24 10:57:21.999 13043-13043/com.chen.demo E/onActivityCreated===: com.chen.demo.MainActivity_3@f2b18ec 02-24 10:57:22.003 13043-13043/com.chen.demo E/topActivity_3: ComponentInfo{com.chen.demo/com.chen.demo.MainActivity_3} 02-24 10:57:22.003 13043-13043/com.chen.demo E/onActivityStarted===: com.chen.demo.MainActivity_3@f2b18ec 02-24 10:57:22.003 13043-13043/com.chen.demo E/onActivityResumed===: com.chen.demo.MainActivity_3@f2b18ec 02-24 10:57:22.382 13043-13043/com.chen.demo E/onActivityStopped===: com.chen.demo.MainActivity_2@53c82a7