2

I want to manage all activities with class conductor like this: enter image description here
Also all activities extend base activity to use common view.
In this case, I want to handle transfer activity, for example:

 Base -> First -> Second -> Third -> First
 Base -> First -> Fourth -> Fifth -> Fourth

When transferring activity, Conductor must handle all activity in stack.
I try to write this conductor as below (I use list to manage instead of stack):

public class Conductor {    
   private List<Activity> listOfActivityInStack;  

   public Conductor(){
  listOfActivityInStack = new ArrayList<Activity>();
   }
   public void startActivity(Activity activity, Class<?> cls){
  listOfActivityInStack.add(activity);
  Intent i = new Intent(activity.getApplicationContext(), cls);
  activity.startActivity(i);
}

   public void startActivityForResult(Activity activity, Class<?> cls, int requestCode){
  listOfActivityInStack.add(activity);
  Intent i = new Intent(activity.getApplicationContext(), cls);
  activity.startActivityForResult(i, requestCode);
}

   public void startAcitivtyClearPrevious(Activity activity, Class<?> cls){
  listOfActivityInStack.clear();
  listOfActivityInStack.add(activity);
  Intent i = new Intent(activity.getApplicationContext(), cls);
  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  activity.startActivity(i);
}

   public int getCount(){
  if(listOfActivityInStack == null)
    return 0;
  return listOfActivityInStack.size();
 }
 }

I store this conductor in Global variable. Then I use it as below:

  //Get conductor from application global
  conductor.startActivity(FirstActivity.this, SecondActivity.class);
  //Then add conductor to application global

But I have some problem:

  • I must handle goBack() for all activity to remove activity from list.
  • Check activity has exist in list, if yes, try get its instance.

Is there best way to manage all activity on android? I have tried search but not found good answer.
I wonder weather or not my way is right. Any recommend or example would be help!

Community
  • 1
  • 1
ductran
  • 10,043
  • 19
  • 82
  • 165
  • 1
    If any `Activity` can 'move' to another `Activity` in an undefined order then you can have each `Activity` call `startActivity(...)` and then immediately call `finish()` which will mean the `Activity` starting the other one will self-terminate. – Squonk Jul 16 '12 at 04:38
  • But I want to reuse activity. Each activity has some states, do I have to save and restore these states? – ductran Jul 16 '12 at 06:25
  • 1
    The best way to manage all activity on android is explained in the official dev guide - [Tasks and Back Stack](http://developer.android.com/guide/components/tasks-and-back-stack.html). – yorkw Jul 19 '12 at 22:15
  • @yorkw I have already read this document, I understand how it does. But I don't know how to make activities task work smoothly. Could you give me an example in real world? – ductran Jul 20 '12 at 02:48
  • What do you mean by _smoothly_? Do you mean that you have lags when switching Activities or what? It seems you are trying to implement something that the platform already gives you. If you have a good reason for doing that, please elaborate. – Marcin Koziński Jul 20 '12 at 10:05
  • Maybe I make a mistake, I don't want implement something that the platform already gives me, I just want a manager class that take care all activities transference (like a helper), for example, when transfer from B to A, if activity A exists in task, the manager just recall it (Activity B just call manager class to transfer to A, and don't care how to do that). But if there is noway to do that (or OS support better), please make an answer and I will accept it. Thanks – ductran Jul 20 '12 at 15:48

1 Answers1

1

there is one way to do this. you must save your all activities state. and when you need to recall them you must use that state.

for extra info look here:

Saving Android Activity state using Save Instance State

Community
  • 1
  • 1
bmavus
  • 892
  • 1
  • 7
  • 21
  • I have already known it. I'm looking for a better way but doesn't help. I accepted your answer because you are the only one answered my question. Thanks! – ductran Jul 24 '12 at 06:16