0

I want to do different operations in onCreate() depending activities I come from.

I do that making this:

Activity A: 
   intent to activity B; 
   intent.putBoolean("name",boolean=false); 
   startActivity B.

Activity B: 
onCreate(){
   Boolean.getBoolean("name"); 
   if(boolean){
   }else{**make this**} 
on the other hand if click on a Button 
   onClick {starts activity C}.

Activity C: 
do things... 
-> if click on a Button -> 
onClick { 
   intent.putBoolean(boolean=true);
   starts ActivityB}

Activity B onCreate(){ 
   if(boolean){**make this**
   } else{}

My question: Can I do the same thing of better way?

Thanks

Donato Szilagyi
  • 4,279
  • 4
  • 36
  • 53
Dors
  • 5
  • 1
  • 3

1 Answers1

0

You can use the putExtra attribute of the Intent to pass the name of the Activity that is launching the current activity. For example, if activity A is calling C, then you would do this in the calling activity:

    Intent intent = new Intent(this, C.class);
    intent.putExtra("activity","A");
    startActivity(intent);

In Activity C:

    Intent intent = getIntent();
    String activity = intent.getStringExtra("activity");

Now in the string activity you will get A and you will know you are coming from A. If you do the same for B, then your activity string will read B.

If C can only be launched via A or B, then you do not have any more to deal with but if it could be launched from HOME for example, then you might need an extra check in C.

Erol
  • 6,478
  • 5
  • 41
  • 55