【问题标题】:How to refresh the fragment onResume如何刷新片段onResume
【发布时间】:2021-12-28 09:49:35
【问题描述】:

我是 Android 开发的新手。我面临一个问题。我需要你们的帮助。

首先,我正在开发一个类似于 BookMyShow 的应用程序。

我有一个包含片段的活动。该活动包含回收站视图中的时间段列表。

当我点击其中一个时间段时,我会转到新活动,在其中我使用用户名、id、timeslot_selected 等特定详细信息预订节目,一旦他在活动中提交“提交”按钮,它就会出现回到他之前所在的同一个片段,并在片段上显示他的详细信息。最初在回收站视图中没有人,现在用户应该在选定的时间段在回收站视图中看到自己作为图标。

我也有下一个按钮。当用户单击它时,我将片段替换为以下日期。如果用户单击其中一个插槽,他将被重定向到相同的预订活动,并且一旦他提交,他应该返回到前一个片段,其日期与他之前所在的日期相同,并带有更新的他自己的图标。

现在,一旦他在预订活动上单击提交,我就无法在上一个片段中显示更新的图标。你能建议我该怎么做吗?

我尝试分离和附加片段,但无济于事。请帮我解决。提前致谢!

SampleActivity.java:

public class SampleActivity extends AppCompatActivity {

TextView text;
Person person;
String id, name, date;


@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);

    Intent intent = getIntent();
    id = intent.getStringExtra("id");
    name= intent.getStringExtra("name");
    date= intent.getStringExtra("date");
    person= (Person) getIntent().getSerializableExtra("person");


    text= findViewById(R.id.name);
    text.setText(name);

    Bundle bundle = new Bundle();
    bundle.putString("id", id);
    bundle.putString("name", name);
    bundle.putString("date", date);
    bundle.putSerializable("Person", person);

    FragmentA newFragment = new FragmentA();
    newFragment.setArguments(bundle);
    getSupportFragmentManager().beginTransaction().replace(R.id.mainFrameLayout, newFragment, "main_fragment").commit();

}

FragmentA.java:

public class FragmentA extends Fragment implements View.OnClickListener {

RecyclerView recyclerView;

public FragmentA() {
}

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view =  inflater.inflate(R.layout.fragment_new, container, false);

    recyclerView = view.findViewById(R.id.slots);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    requestQueue = VolleySingleton.getmInstance(getActivity()).getRequestQueue();
    newList = new ArrayList<>();


    next = view.findViewById(R.id.nextButton);
    timeView = view.findViewById(R.id.timeOn);
    dateText= view.findViewById(R.id.date);

    Bundle bundle = getArguments();
    assert bundle != null;
    id= bundle.getString("id");
    name = bundle.getString("name");
    date = bundle.getString("date");
    person = (Person) bundle.getSerializable("Person");

    next.setOnClickListener(this);


        return view;
}


@Override
public void onClick(View v) {

    if (v.getId()== R.id.nextButton) {
        replaceFragment();
    }

}



private void replaceFragment() {

    Bundle bundle = new Bundle();
  bundle.putString("id", id);
  bundle.putString("name", name);
  bundle.putString("date", date);
  bundle.putSerializable("Person", person);
   bundle.putSerializable("hashmap", (Serializable) hashmap);

    fm = requireActivity().getSupportFragmentManager();
    ft = fm.beginTransaction();
    f = new FragmentFirst();
    f.setArguments(bundle);
    ft.replace(R.id.fragmentLayout, f, "main_fragment");
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(null);
    ft.commit();


}

@Override
public void onAttach(@NonNull Context context) {
    super.onAttach(context);
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}



public void refreshData() {

    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    Fragment currentFragment = fragmentManager.findFragmentByTag("main_fragment");
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    if(currentFragment != null) {
        fragmentTransaction.detach(currentFragment);
        fragmentTransaction.attach(currentFragment);
        fragmentTransaction.commit();
    }
  }
}

【问题讨论】:

  • 使用 onAttach 方法

标签: java android android-fragments android-activity android-recyclerview


【解决方案1】:

正如我所见,您正试图在预订活动中转到 FragmentA 的新实例。如果您想将数据发送到活动,处理数据并返回结果,在您的情况下,您可以通过startForActivityResult 为结果启动新活动来完成,并在完成活动之前使用setResult 方法设置结果您的预订活动。在这种情况下,您可以通过onActivityResult 方法使用结果。但我建议您在预订过程中也使用片段而不是活动。

【讨论】:

  • 您好,谢谢您的回复。我完全按照您之前所说的使用了 setResult 方法,并且得到了正确的输出。但是,对于嵌套的片段,它不会出现。我的意思是只对 FragmentA 它即将到来。对于以下片段,它不会更新。任何想法,我需要改变什么。
  • 如果你想将结果放在另一个现在不存在于堆栈中的片段中,你可以传递保持并传递你在片段A中收到的数据。但是在这种情况下,例如 fragmentB 是活动的,您可以直接在它的 onActivityResult 方法中获取结果。我希望我能帮上忙。但是,如果您对此有任何问题,可以将您的源代码发送给我,我可以帮助您更轻松地更正它。
猜你喜欢
  • 1970-01-01
  • 2013-02-26
  • 1970-01-01
  • 1970-01-01
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多