简体   繁体   English

如何刷新片段onResume

[英]How to refresh the fragment onResume

I am a newbie in Android development.我是 Android 开发的新手。 I am facing an issue.我面临一个问题。 I need some help from you guys.我需要你们的帮助。

Firstly, I am working on an application that is similar to BookMyShow.首先,我正在开发一个类似于 BookMyShow 的应用程序。

I have an activity that contains a fragment.我有一个包含片段的活动。 The activity contains a list of timeslots in a recycler view.该活动包含回收站视图中的时间段列表。

When I click on one of the timeslots, I go to new activity where I book the show with the specific details like username, id, timeslot_selected, etc and once he submits the "SUBMIT" button in the activity, it should come back to the same fragment that he was in, earlier, and show his details on the fragment.当我点击其中一个时间段时,我将 go 加入新活动,在其中我使用用户名、id、timeslot_selected 等特定详细信息预订节目,一旦他在活动中提交“提交”按钮,它应该会回到与他之前所在的片段相同,并在片段上显示他的详细信息。 Initial there is nobody in the recycler view, now the user should see himself in that recycler view at the selected timeSlot as an icon.最初在回收站视图中没有人,现在用户应该在选定的时间段在回收站视图中看到自己作为图标。

I have the next button as well.我也有下一个按钮。 When the user clicks on it, I am replacing the fragment with the following date.当用户单击它时,我将片段替换为以下日期。 If the user clicks on one of the slots, he would be redirected to the same booking activity and once he submits, he should come back to the previous fragment with the same date that he was on earlier and with an updated icon of himself.如果用户单击其中一个插槽,他将被重定向到相同的预订活动,并且一旦他提交,他应该返回到前一个片段,该片段具有与他之前相同的日期和更新的他自己的图标。

Now, I am unable to show that updated icon in the previous fragment once he clicks submit on the booking activity.现在,一旦他在预订活动上单击提交,我就无法在上一个片段中显示更新的图标。 Can you please suggest what should I do?你能建议我该怎么做吗?

I tried detaching and attaching the fragment but to no avail.我尝试分离和附加片段,但无济于事。 Please help me with a solution.请帮我解决。 Thanks in Advance!提前致谢!

SampleActivity.java: 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:片段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();
    }
  }
}

As I see you are trying to go to a new instance of FragmentA in your booking activity.正如我所看到的,您正在尝试在预订活动中将 go 转换为 FragmentA 的新实例。 If you want to send data to an activity, process the data and return the result, in your case you could do it by starting new activity for result by startForActivityResult and also set the result before finish the activity with setResult method in your booking activity.如果您想将数据发送到活动,处理数据并返回结果,在您的情况下,您可以通过startForActivityResult为结果启动新活动并在完成活动之前在预订活动中使用setResult方法设置结果。 In this case you can use the result via onActivityResult method.在这种情况下,您可以通过onActivityResult方法使用结果。 But I suggest you to use fragment for the booking process as well instead of activity.但我建议您在预订过程中也使用片段而不是活动。

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

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