简体   繁体   English

Android:点击按钮时从另一个活动刷新/更新片段

[英]Android: refresh/update fragment from another activity on button clicked

I have a Dialog prompt in mainActivity .我在mainActivity有一个 Dialog 提示。 If user close the Dialog, I need to show red dot to one of the ListView items inside fourthFragment (as per redbox in fourthFragment in image below).如果用户关闭该对话框,我需要表现出红点里面的ListView的项目之一fourthFragment (按红盒子在fourthFragment如下图)。

The problem is the red dot only be updated after I close the app and reopened it, because fourthFragment already done created BEFORE user close the Dialog.问题是红点只有在我关闭应用程序并重新打开它后才会更新,因为在用户关闭对话框之前已经创建了fourthFragment How can I refresh/update the fourthFragment after Dialog closed so that red dot can be shown immediately?如何在对话框关闭后刷新/更新fourthFragment ,以便立即显示红点?

short description:简短的介绍:

  • mainActivity : on Dialog closed > store showRedDot ="1" to local db mainActivity :在对话框关闭时 > 将showRedDot ="1" 存储到本地数据库
  • fourthFragment : onCreate > read showRedDot from local db, if "1", show red dot. fourthFragment :的onCreate>阅读showRedDot从本地数据库中,如果“1”,显示红点。 ( problem here, when onCreate, showRedDot is still "0", so I need to update fourthFragment layout after dialog closed.) (这里的问题,当onCreate时, showRedDot仍然是“0”,所以我需要在对话框关闭后更新fourthFragment布局。)

在此处输入图片说明

You have calling a interface to push event dismiss(button click) of dialog.您已经调用了一个接口来推送对话框的事件解除(按钮单击)。 Like this:像这样:

public interface MyDialogListener {
    void OnCloseDialog();
}

public class fourthFragment extend Fragment implements MyDialogListener {
    public void SomeMethod() {
        MyDialog myDialog = new MyDialog(this, this);
        myDialog.show();
    }

    public void OnCloseDialog() {
        // Do update your listview in here( maybe call method initialize data for listview)
    }

}

public class MyDialog extends Dialog {
    MyDialogListener mListener;

    public MyDialog (Context context, MyDialogListener listener) {
        super(context, R.style.Dialog);
        mListener = listener;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.CloseButton:
                // Push event when Dialog close(or anything)
                mListener.OnCloseDialog();
                dismiss()
                break;
            default:
                //...
        }
    }
}

Try Interface or Broadcast receivers as above.尝试上面的接口或广播接收器。 If else refresh the fragment as如果否则刷新片段为

    // Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();

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

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