I want to get the Instance Of the activities which are currently open? Is There any way to get the instance Of all the activities Through the ActivityManager.I think ActivityManager only gives the name of the activities.
Asked
Active
Viewed 32 times
1 Answers
0
No!, there is not an way to do this.
There is an way to get name of current activity not the instance because you may not own that activity.
Instead you can keep reference of your Activities, this example shows way to manage single reference, but you can set your logic for having all live activities.
<application
android:name=".MyApp"
....
</application>
Your application class :
public class MyApp extends Application {
public void onCreate() {
super.onCreate();
}
private Activity mCurrentActivity = null;
public Activity getCurrentActivity(){
return mCurrentActivity;
}
public void setCurrentActivity(Activity mCurrentActivity){
this.mCurrentActivity = mCurrentActivity;
}
}
Create a new Activity :
public class MyBaseActivity extends Activity {
protected MyApp mMyApp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMyApp = (MyApp)this.getApplicationContext();
}
protected void onResume() {
super.onResume();
mMyApp.setCurrentActivity(this);
}
protected void onPause() {
clearReferences();
super.onPause();
}
protected void onDestroy() {
clearReferences();
super.onDestroy();
}
private void clearReferences(){
Activity currActivity = mMyApp.getCurrentActivity();
if (this.equals(currActivity))
mMyApp.setCurrentActivity(null);
}
}
Get the live activity
Activity currentActivity = ((MyApp)context.getApplicationContext()).getCurrentActivity();
Now change the code for all live activities.

Khemraj Sharma
- 57,232
- 27
- 203
- 212