Android - How to get the value of the parentActivityName attribute programmatically?

Activity is declared in my manifest like this:

    <activity
        android:name=".TicketingHomeActivity"
        android:label="@string/title_activity_ticketing_home"
        android:parentActivityName=".HomeActivity" />

      

How can I get the value android:parentActivityName

programmatically?

I need to do this as I just replaced my ActionBar in the Toolbar but getSupportActionBar().setDisplayShowHomeEnabled(true)

now shows an up arrow in the Activity even if there is no parentActivityName attribute in the manifest. So I would like to detect in my BaseActivity if parentActivityName has not been set, so I can hide the up arrow accordingly.

NB - This question is not a duplicate of this question as I am specifically asking how to programmatically determine the value of an attribute android:parentActivityName

.

+1


source to share


2 answers


You can call getParentActivityIntent()

your BaseActivity.

It will return intent. Then you can call getComponent()

that will return ComponentName

where you can get android:parentActivityName

(class name) from.

EDIT



Sample code:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {

        toolbar.setTitle(getTitle());

        Intent parentActivityIntent = getParentActivityIntent();
        boolean doesParentActivityExist;
        if (parentActivityIntent == null) {
            doesParentActivityExist = false;
        }
        else {
            ComponentName componentName = parentActivityIntent.getComponent();
            doesParentActivityExist = (componentName.getClassName() != null);
        }
        //toolbar.setSubtitle("Has parent: " + doesParentActivityExist);

        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null && doesParentActivityExist) {

            actionBar.setDisplayHomeAsUpEnabled(doesParentActivityExist);
            actionBar.setHomeButtonEnabled(doesParentActivityExist);

            actionBar.setDefaultDisplayHomeAsUpEnabled(doesParentActivityExist);
            actionBar.setDisplayShowHomeEnabled(doesParentActivityExist);

        }
    }

      

+1


source


It's just that if you don't set the attribute to manifest

, then don't use a method setDisplayShowHomeEnabled()

for that action.
Otherwise one way is that you can put the name of the parent activity in putExtra in the parent activity (the calling activity) and get it in the child activity (the calling activity)

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
     intent.putExtra("PARENT_ACTIVITY_NAME", "ThisActivityName");
     startActivity(intent);

      

And then get in the form of a child



String parentActivityName = intent.getStringExtra("PARENT_ACTIVITY_NAME");

      

And for that, This answer may work as well.

0


source







All Articles