I have three activities - activity A ,activity B ,activity C . Activity A is the launcher activity . From this activity , I go to activity B .
The definition of activity B and activity C is as follows :
public class B extends C
public abstract class C extends BaseActivity
Now I want to do something in activity B after completion of some task in activity C . I have searched in the google and stackoverflow . This leads me to this answer.
But following the above answer the call of function stateChanged() takes a lot of time . Is there any faster way to do this ?
Every B
activity is a C
activity. So if you define a method in C
that is inherited ie the method is public or protected Or package-private with B in same package then override the method as following:
@Override
protected void method (){
super.method ();
myTaskAfterSuperMethodReturns ();
}
If you are doing some async task better you define async task in class B.
Another approach is define doSomething
in Class C
as an obstract method and implement it in B
. Call doSomething
when your task is done. Making doSomething
abstract will force B
to implement it.
您可以在活动C中完成任务时使用EventBus库在活动B中触发事件
You can also access the method of MainActivity from another Activity like this:
MainActivity.class
private static MainActivity mInstance;
public static MainActivity getInstance(){
return mInstance;
}
In Oncreate method initialize the instance
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInstance = this;
}
creating method to use in next activity
@Override
public void showData(){
//do something here
}
To access it just call from another activity
(MainActivity.getInstance()).showData()
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.