[英]Refresh the Activity from Fragment in Android
I have followed this . 我遵循了这一点 。 I want to refresh the
Activity
from Fragment
. 我想从
Fragment
刷新Activity
。 I have back button in toolbar
for Fragment
to Activity
communication. 我在
toolbar
有“后退”按钮,用于“ Fragment
到Activity
通讯。
Here is my code for button listener and refresh the activity. 这是我的按钮侦听器代码并刷新活动。 I include this in fragment class:
我将此包含在片段类中:
final Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Handler().post(new Runnable() {
@Override
public void run()
{
Intent intent = getActivity().getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_NO_ANIMATION);
getActivity().overridePendingTransition(0, 0);
getActivity().finish();
getActivity().overridePendingTransition(0, 0);
startActivity(intent);
}
});
}
});
This works fine, but it completly reload the app. 这可以正常工作,但是可以完全重新加载应用程序。 What can be done here, so that I can avoid the reload operation and just refresh the activity?
在这里可以做什么,这样我就可以避免重新加载操作,而只是刷新活动?
Use callback(interface) to communicate with activity from fragment and don't use Intent.FLAG_ACTIVITY_CLEAR_TOP | 使用回调(接口)与片段中的活动进行通信,不要使用Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK because it will clear your stack.
Intent.FLAG_ACTIVITY_NEW_TASK,因为它将清除堆栈。
ex. 例如
Fragment{
CallBack callback;
// your button click
onClick(){
callBack.mesage("Hi Activity");
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if ( context instanceof Callback ) {
callback = (Callback) context;
} else {
throw new RuntimeException(context.getClass().getSimpleName()
+ " must implement Callback");
}
}
public interface Callback(){
void message(String message);
}
Activity implements Callback{
@Overide
public void message(String message){
System.out.println(message);
// to refresh activity
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
}
}
Define a method which do refresh operations in your activity. 定义在活动中执行刷新操作的方法。
public void refreshMyData(){
// do your operations here.
}
And in your onClick method: 在您的onClick方法中:
final Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// If you use a generic toolbar you have to check instance of getActivity before cast.
((YourActivity)getActivity()).refreshMyData();
}
});
Edit : I refactored the code. 编辑 :我重构了代码。 I think you do not need post in onClick method.
我认为您不需要在onClick方法中发布。
Put this in your fragment 把它放在你的片段中
public void refreshActivity(){
ActivityName mDashboardActivity = (ActivityName) getActivity();
if(ActivityName!=null){
ActivityName.refreshMyData();
}
}
and put refreshMyData in your Activity 并在您的活动中添加refreshMyData
public void refreshMyData(){
//Code to refresh Activity Data hear
}
Let me know if not Working 让我知道是否无法正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.