如何实现getSupportParentActivityIntent()来动态设置活动长达按钮在安卓长达、如何实现、按钮、动态

由网友(相忘于江湖)分享简介:如何知道哪些父活动要求孩子活动的android?How to know which parent activity calls the child activity in android?假设我有三个活动A,B和C. A和B是父母的活动。 C是子活动。这意味着C可通过从A或B中启动。Assume I have t...

如何知道哪些父活动要求孩子活动的android?

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

假设我有三个活动A,B和C. A和B是父母的活动。 C是子活动。这意味着C可通过从A或B中启动。

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?

现在的问题是,我需要根据我想重写 getSupportParentActivityIntent()方法父intent.For设置后退按钮,它需要的意图对象重新开始父活动。

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.

这里是android的网站的方法说明

如何获得父活动,以正确覆盖getSupportParentActivityIntent()方法?

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

感谢。

推荐答案

我已经找到了如何实现getSupportParentActivityIntent()正确使用它,我们可以动态地设置活动上行按钮的机器人。在这里,我是如何做到了这一点。

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.

假设我们有两项活动。活动A和B. A是父活动,B是孩子。

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

所以,有必要建立一个意图开始B.它应该通过一个额外的数据,是父活动的名称。在这里,在我们的例子应该是A。这里是code,

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'

现在在b活动,我们需要重写getSupportParentActivityIntent(),它应该是这样的,

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;
}
阅读全文

相关推荐

最新文章