4

How to know which parent activity calls the child activity in android?

Assume I have three activities A,B and C. A and B are parent activities. C is the child activity. That means C can be started by either from A or B.

So how can I know which parent activity causes to start the child activity?

The problem is I need to set the back button according to that parent intent.For that I want to override getSupportParentActivityIntent() method and it needs the intent object to start again the parent activity.

Here is the method description in android site

How to get the parent activity to correctly override the getSupportParentActivityIntent() method?

Thanks.

chathura
  • 3,362
  • 6
  • 41
  • 68

2 Answers2

2

I have found how to implement getSupportParentActivityIntent() correctly and using it we can dynamically set the activity to up-button in android. Here how I have achieved it.

Assume we have two activities. Activity A and B. A is the parent activity and B is the child.

So A need to create an intent to start B. It should pass an extra data which is the name of the parent activity. Here in our example it should be 'A'. Here is the code,

Intent intent = new Intent();
intent.putExtra("ParentClassName","A");
startActivity(intent.setClass(A.this, B.class)); //we are starting activity 'B'

Now in activity B we need to override getSupportParentActivityIntent() and it should look like this,

@Override
public Intent getSupportParentActivityIntent() {
    Intent parentIntent= getIntent();
    String className = parentIntent.getStringExtra("ParentClassName"); //getting the parent class name

    Intent newIntent=null;
    try {
         //you need to define the class with package name
         newIntent = new Intent(B.this,Class.forName("com.myapplication."+className));

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return newIntent;
}
chathura
  • 3,362
  • 6
  • 41
  • 68
  • Exact duplicate of [your answer](http://stackoverflow.com/questions/19843757/how-do-i-set-the-activity-of-the-android-up-button-dynamically/22089876#22089876), stop posting duplicate answers and there by not creating redundancy. – Paresh Mayani Feb 28 '14 at 16:04
  • Ok, I put the link of this answer there. – chathura Mar 02 '14 at 07:08
  • If you always want to return to the calling activity you don't need to use the PutExtra when you create activity B. Instead in the getSupportParentActivityIntent() you can create the new intent using: newIntent = new Intent(B.this, getIntent().getClass()); – leafcutter Aug 25 '14 at 12:42
0

In ur child activity use like

@Override
public Intent getSupportParentActivityIntent() {
    String from = getIntent().getExtras().getString("from");
    Intent newIntent = null;
    if(from.equals("MAIN")){
        newIntent = new Intent(this, MainActivity.class);
    }else if(from.equals("FAV")){
        newIntent = new Intent(this, FavoriteActivity.class);
    }
    ...
    return newIntent;
}

before that u need to put from extra value for each of ur source activity say from FavoriteActivity

i.putExtra("from", "FAV");

and for MainActivity use

i.putExtra("from", "MAIN");
Abhijit
  • 360
  • 4
  • 14