简体   繁体   中英

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.

Now when i want to restart the Activity_A from the class_B. 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.

i want to replace Activity_A.class with the Activity which called class_B

In your case you shoud pass a class variable togethe with Context of activity. eg from Actitity_A:

class_B.doMethod(this,Activity_A.class);

And use class variable for start Activity from class_B

关于使用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.

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

and from ClassB, you can basically check Activity Names in calledFrom String and start Activity Accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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