Android-如何以编程方式获取parentActivityName属性值? [英] Android - How to get parentActivityName attribute value programmatically?

查看:96
本文介绍了Android-如何以编程方式获取parentActivityName属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的清单中声明了一个活动,如下所示:

An activity is declared in my manifest like this:

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

如何以编程方式获取android:parentActivityName的值?

How can I get the value of android:parentActivityName programmatically?

我需要执行此操作,因为我刚刚用工具栏替换了ActionBar,但是即使清单中未设置parentActivityName属性,getSupportActionBar().setDisplayShowHomeEnabled(true)现在仍在活动中显示向上箭头.因此,我想在我的BaseActivity中检测是否未设置parentActivityName,以便可以相应地隐藏向上"箭头.

I need to do this as I have just replaced my ActionBar with a Toolbar, but getSupportActionBar().setDisplayShowHomeEnabled(true) is now showing the Up arrow in an activity even when there is no parentActivityName attribute set in the manifest. Therefore, I would like to detect in my BaseActivity if no parentActivityName has been set, so I can hide the Up arrow accordingly.

NB-此问题与此问题无关. a>,因为我专门询问如何以编程方式检测android:parentActivityName属性的值.

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

推荐答案

您可以在BaseActivity上调用getParentActivityIntent().

You can call getParentActivityIntent() on your BaseActivity.

它将返回一个Intent.然后,您可以调用getComponent(),这将返回一个ComponentName,您可以从其中获取android:parentActivityName(类名).

It will return an Intent. Then you can call getComponent() that will return a ComponentName from where you can obtain the android:parentActivityName (classname).

编辑

代码示例:

    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);

        }
    }

这篇关于Android-如何以编程方式获取parentActivityName属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆