繁体   English   中英

如何刷新片段onResume

[英]How to refresh the fragment onResume

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

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

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

当我点击其中一个时间段时,我将 go 加入新活动,在其中我使用用户名、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();

}

片段A.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();
    }
  }
}

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

暂无
暂无

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

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