Reputation: 1
public class MyApplication extends Application {
ArrayList<Activity> mActivityList = new ArrayList<Activity>();
public void addActivity(Activity a){
mActivityList.add(a);
}
public void removeActivity(Activity a){
for(...){
...name equals..
a.finish();
}
}
public void removeAll(){
for(){
...
a.finish();
}
}
public Activity getActivity(){...};
}
It is a very easy way to manage the activities, but the heap memory of the mActivityList cost much. so is there another way to manage the activities
Upvotes: 0
Views: 52
Reputation: 3236
Tracking activities in a list is definitely not a good way.
If your only intention of doing so is to finish all (or any) of them conveniently, I suggest you take a look into BroadcastReceiver
Here's a snippet you can include into all your activities:
private BroadcastReceiver finishBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Start listening to any broadcast "tagged" with action name actionFinish.
LocalBroadcastManager.getInstance(this).registerReceiver(finishBroadcastReceiver,
new IntentFilter("actionFinish"));
}
@Override
public void onDestroy() {
super.onDestroy();
// Stop listening to broadcast when the activity is destroyed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(finishBroadcastReceiver);
}
And do this whenever you want to finish all the activities:
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("actionFinish"));
Manipulate the Intent object by using Intent.putExtra() to identify which Activity you wish to dispose, if not all.
Upvotes: 1