我有3个活动,活动A有3个片段,其中一个片段与另一个片段重叠,在所有片段中,我都有一个列表视图。准确返回以下路径的最佳方法是什么:活动A从片段A调用Listview,从片段B调用ListView,从片段B调用ListView,从片段C调用ListView,又依次从活动B调用。活动B中的返回按钮,我希望它返回与片段C相同的ListView,后者返回与片段B ....相同的ListView,依此类推。我可以使用addToBackStack (null)
在活动A中从片段A到C进行往返,但是从活动B中我只能以OnBackPressed ()
返回到片段A中的活动A;
有人可以给我解决方案吗?
注意:这些ListView是我从SQLite数据库获取的数据;
活动A:
public class ActivityA extends AppCompatActivity {
private Context context;
private Fragment nFA;
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen);
context = getBaseContext();
//
nFA = new FragmentA();
//
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.ll_fragment, nFA, "f1name");
ft.commit();
}
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
Intent mIntent = new Intent(context, MenuActivity.class);
startActivity(mIntent);
finish();
super.onBackPressed();
}
}
}
片段A:
public class FragmentA extends Fragment {
private ListView lv_fragment;
private View view;
private Fragment nFB;
private HMAuxN item;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment, container, false);
lv_fragment = view.findViewById(R.id.lv_fragment);
NDao nDao = new NDao(getContext());
//
MyAdapterData1 adapter = new MyAdapterData1(getContext(), R.layout.celula_listview_data, nDao.getListData1());
lv_fragment.setAdapter(adapter);
nFB = new FragmentB();
lv_fragment.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
item = (HMAuxN) parent.getItemAtPosition(position);
//
String data1 = (item.get(NDao.DATA1));
Bundle bundle = new Bundle();
bundle.putString("data1", data1);
nFB.setArguments(bundle);
//
FragmentManager fm = getFragmentManager();
FragmentTransaction ft;
if (fm != null) {
ft = fm.beginTransaction();
ft.replace(R.id.ll_fragment, nFB);
ft.addToBackStack(null);
ft.commit();
}
}
});
return view;
}
}
片段B:
public class FragmentB extends Fragment {
private ListView lv_fragment;
private View view;
private Fragment nFC;
private HMAuxN item;
String data2;
String data1;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment, container, false);
lv_fragment = view.findViewById(R.id.lv_fragment);
NDao nDao = new NDao(getContext());
Bundle bundle = this.getArguments();
assert bundle != null;
data1 = bundle.getString("data1");
//
MyAdapterData2 adapter = new MyAdapterData2(getContext(), R.layout.celula_listview_data, nDao.getListData2(data1));
lv_fragment.setAdapter(adapter);
nFC = new FragmentC();
lv_fragment.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
item = (HMAuxN) parent.getItemAtPosition(position);
//
data2 = (item.get(NDao.DATA2));
Bundle bundle = new Bundle();
bundle.putString("data1", data1);
bundle.putString("data2", data2);
nFC.setArguments(bundle);
//
FragmentManager fm = getFragmentManager();
FragmentTransaction ft;
if (fm != null) {
ft = fm.beginTransaction();
ft.replace(R.id.ll_fragment, nFC);
ft.addToBackStack(null);
ft.commit();
}
}
});
return view;
}
}
片段C:
public class FragmentC extends Fragment {
private ListView lv_fragment;
private View view;
private Activity activity;
public void onAttach(Context context) {
super.onAttach(context);
activity = (Activity) context;
}
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment, container, false);
lv_fragment = view.findViewById(R.id.lv_fragment);
NDao nDao = new NDao(getContext());
Bundle bundle = this.getArguments();
assert bundle != null;
String data1 = bundle.getString("data1");
String data2 = bundle.getString("data2");
MyAdapter adapter = new MyAdapter(getContext(), R.layout.celula_listview_layout, nDao.getList(data1, data2));
lv_fragment.setAdapter(adapter);
lv_fragment.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HMAuxN item = (HMAuxN) parent.getItemAtPosition(position);
//
Intent mIntent = new Intent(getContext(), ActivityB.class);
mIntent.putExtra("id", item.get(NDao.IDNOTAS));
startActivity(mIntent);
//
activity.finish();
}
});
return view;
}
}