简体   繁体   English

如何使用Context重新启动调用了其他类的活动?

[英]How to restart an activity which called some other class using Context?

When i am in an Activity say Activity_A, i go to a class_B using the context of Activity_A. 当我在Activity中说Activity_A时,我会使用Activity_A的上下文进入class_B。

Now when i want to restart the Activity_A from the class_B. 现在,当我想从class_B重新启动Activity_A时。 I use this 我用这个

Intent intent = new Intent(context, Activity_A.class);
context.startActivity(intent);

what i want is when i am calling the class_B from various Activities, it must restart the Activity it is called from, using the context which is sent to the class_B from the Activity. 我想要的是当我从各种Activity调用class_B时,它必须使用从Activity发送到class_B的上下文重新启动从其调用的Activity。

i want to replace Activity_A.class with the Activity which called class_B 我想用称为class_B的Activity替换Activity_A.class

In your case you shoud pass a class variable togethe with Context of activity. 在您的情况下,您应该传递一个类变量和活动上下文一起使用。 eg from Actitity_A: 例如来自Actitity_A:

class_B.doMethod(this,Activity_A.class);

And use class variable for start Activity from class_B 并使用class变量从class_B启动Activity

关于使用startActivityForResult(intent,code)而不是仅仅使用startActivity(intent)呢?

You can pass a string containing name of the Activity. 您可以传递包含活动名称的字符串。 like below 像下面

Intent intent = new Intent(context, Activity_A.class);
intent.putExtra("CALLED_FROM","ACTIVITY_A"); //PASSING FLAG Variable to know from where it called
context.startActivity(intent);

' '

in onCreate of Activity_B you can use below code. 在Activity_B的onCreate中,您可以使用以下代码。

String calledFrom = getIntent().getStringExtra("CALLED_FROM");

and from ClassB, you can basically check Activity Names in calledFrom String and start Activity Accordingly. 从ClassB,您基本上可以检查namedFrom字符串中的活动名称,并相应地启动Activity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM