[英]navigation drawer back button stack bug
我有一個帶有導航抽屜菜單的應用程序,問題是當我按下“后退”按鈕時。
我在抽屜上有2個菜單=片段A,片段B我轉到片段A1,然后轉到A2,然后移至片段B。然后按片段B上的返回按鈕,屏幕返回到片段A。我想如果我按片段B上的后退按鈕,則該應用程序已關閉。 誰能幫我
這是我的代碼
片段A1
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("ZZZ", "ada di oncreateView maintain");
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_layout_schedule_day, container, false);
final List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<6;i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("day", days[i]);
aList.add(hm);
// R.layout.listview_layout defines the layout of each item
list = (ListView) rootView.findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(getActivity(), aList, R.layout.list_schedule_day, new String[]{"day"}, new int[]{R.id.day});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String day = ((TextView) view.findViewById(R.id.day)).getText().toString();
// Intent in = new Intent(getActivity(),FragmentScheduleDetail.class);
// in.putExtra("txt",day);
Fragment fragment = new FragmentScheduleRoom();
Bundle data = new Bundle();
data.putString("selectday", day);
fragment.setArguments(data);
FragmentManager frgManager = getFragmentManager();
Log.d("tag", "DAY SELECT: " + data.getString("selectday"));
FragmentTransaction tx = frgManager.beginTransaction();tx.replace(R.id.content_frame, fragment)
.addToBackStack( "schedule" ).commit();
//Toast.makeText(getActivity(), "You Clicked at " + aList.get(+position).get("day"), Toast.LENGTH_SHORT).show();
}
});
}
return rootView;
}
}
片段A2
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("ZZZ", "ada di oncreateView schedule");
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_layout_schedule_detail, container, false);
new JSONParse().execute();
return rootView;
}
B1片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_layout_bad, container, false);
new JSONParse().execute();
return rootView;
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
device_id = (TextView) getActivity().findViewById(R.id.device_id);
name = (TextView) getActivity().findViewById(R.id.name);
location = (TextView) getActivity().findViewById(R.id.location);
status = (TextView) getActivity().findViewById(R.id.status);
device_type= (TextView) getActivity().findViewById(R.id.device_type);
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Getting Bad Device List...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... string) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.makeHttpRequestWithoutParams(url);
Log.d("ZZZ", json.toString());
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
oslist.clear();
try {
// Getting JSON Array from URL
String statusR = json.getString(TAG_STATUS);
if(statusR.equals("success")){
bad = json.getJSONArray(TAG_OS);
for(int i = 0; i < bad.length(); i++){
final JSONObject c = bad.getJSONObject(i);
// Storing JSON item in a Variable
String device_id = c.getString(TAG_DEVICE_ID);
String name = c.getString(TAG_NAME);
String location = c.getString(TAG_LOCATION);
String status = c.getString(TAG_STATUS);
String device_type = c.getString(TAG_DEVICE_TYPE);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_DEVICE_ID, device_id);
map.put(TAG_NAME, name);
map.put(TAG_LOCATION, location);
map.put(TAG_STATUS, status);
map.put(TAG_DEVICE_TYPE, device_type);
oslist.add(map);
list=(ListView) getActivity().findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
R.layout.list_bad,
new String[] {TAG_DEVICE_ID, TAG_NAME,TAG_LOCATION, TAG_STATUS, TAG_DEVICE_TYPE}, new int[] {
R.id.device_id, R.id.name, R.id.location, R.id.status, R.id.device_type});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String device_id = oslist.get(position).get("device_id");
String device_type = oslist.get(position).get("device_type");
Log.d("device_id", "IDP clicked : " + device_id);
Log.d("device_type", "Type clicked : " + device_type);
if (device_type.length()<7)
{
Fragment fragment = new FragmentScanServer();
Bundle data = new Bundle();
data.putString("device_id", device_id);
fragment.setArguments(data);
FragmentManager frgManager = getFragmentManager();
Log.d("CLICK", "Send IDP to Maintenance Add : " + data.getString("device_id"));
// frgManager.popBackStack("tag", FragmentManager.POP_BACK_STACK_INCLUSIVE);
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
}
else
{
Fragment fragment = new FragmentScanNetwork();
Bundle data = new Bundle();
data.putString("device_id", device_id);
fragment.setArguments(data);
FragmentManager frgManager = getFragmentManager();
Log.d("CLICK", "Send IDP to Maintenance Add : " + data.getString("device_id"));
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
}
使用getBackStackEntryAt
從BackStack獲取頂部片段,並檢查用戶是否在F2片段上並按下后退,然后關閉片段容器Activity。 覆蓋onBackPressed()
方法:
@Override
public void onBackPressed() {
FragmentManager fmanger = getSupportFragmentManager();
if(fmanger.getBackStackEntryCount()>0){
FragmentManager.BackStackEntry backEntry=
fmanger.getBackStackEntryAt(fmanger.getBackStackEntryCount()-1);
String fragmentName=backEntry.getName();
if(fragmentName.equals("schedule")){
// clear all fragment from Activity
// close Activity
this.finish();
}
}
}
為此,您必須對導航邏輯進行一些更改。 因此,每當單擊任何菜單項時,都需要清除堆棧中的所有片段並附加新的片段。
透明堆棧方法是
private void clearStack() {
FragmentManager frgManager = getFragmentManager();
frgManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
frgManager.executePendingTransactions();
}
而已。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.