0

我已經創建了一個演示應用程序,它包含菜單抽屜和使用抽屜佈局的同一窗口中的選項卡式活動。問題是當我在抽屜菜單中選擇它重定向其相關片段頁面,但當點擊返回按鈕的標籤活動自動消失,我的應用程序出來了,我該如何解決這個問題?這是我的主要活動代碼Android studio 2.2.3後退按鈕活動

import android.content.Intent; 
import android.support.design.widget.NavigationView; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity { 
    DrawerLayout mDrawerLayout; 
    NavigationView mNavigationView; 
    FragmentManager mFragmentManager; 
    FragmentTransaction mFragmentTransaction; 
    //Button signIn; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
     mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ; 

     /** 
     * Lets inflate the very first fragment 
     * Here , we are inflating the TabFragment as the first Fragment 
     */ 

     mFragmentManager = getSupportFragmentManager(); 
     mFragmentTransaction = mFragmentManager.beginTransaction(); 
     mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit(); 
     /** 
     * Setup click events on the Navigation View Items. 
     */ 

     mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
      @Override 
      public boolean onNavigationItemSelected(MenuItem menuItem) { 
       mDrawerLayout.closeDrawers(); 
       if (menuItem.getItemId() == R.id.nav_item_income) { 
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); 
        fragmentTransaction.replace(R.id.containerView,new IncomeFragment()).commit(); 

       } 

       if (menuItem.getItemId() == R.id.nav_item_expence) { 
        FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction(); 
        xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit(); 
       } 


       return false; 
      } 

     }); 
     /** 
     * Setup Drawer Toggle of the Toolbar 
     */ 

     android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar); 
     ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name, 
       R.string.app_name); 

     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     mDrawerToggle.syncState(); 

    } 

    } 

這是我的標籤活動

package com.example.arunbhaskar.seconddemo; 

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

/** 
* Created by arun.bhaskar on 2/9/2017. 
*/ 

public class TabFragment extends Fragment { 
    public static TabLayout tabLayout; 
    public static ViewPager viewPager; 
    public static int int_items = 3 ; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     /** 
     *Inflate tab_layout and setup Views. 
     */ 
     View x = inflater.inflate(R.layout.tablayout,null); 
     tabLayout = (TabLayout) x.findViewById(R.id.tabs); 
     viewPager = (ViewPager) x.findViewById(R.id.viewpager); 
     /** 
     *Set an Apater for the View Pager 
     */ 

     viewPager.setAdapter(new MyAdapter(getChildFragmentManager())); 
     /** 
     * Now , this is a workaround , 
     * The setupWithViewPager dose't works without the runnable . 
     * Maybe a Support Library Bug . 
     */ 

     tabLayout.post(new Runnable() { 
      @Override 
      public void run() { 
       tabLayout.setupWithViewPager(viewPager); 
      } 
     }); 

     return x; 

    } 
    class MyAdapter extends FragmentPagerAdapter { 

     public MyAdapter(FragmentManager fm) { 
      super(fm); 
     } 



     /** 
     * Return fragment with respect to Position . 
     */ 


     @Override 
     public Fragment getItem(int position) 
     { 
      switch (position){ 
       case 0 : return new Pie chart(); 
       case 1 : return new Graph(); 
       case 2 : return new Transaction(); 
      } 
      return null; 
     } 

     @Override 
     public int getCount() { 
      return int_items; 
     } 
     @Override 
     public CharSequence getPageTitle(int position) { 

      switch (position){ 
       case 0 : 
        return "pie-chart"; 
       case 1 : 
        return "Graph"; 
       case 2 : 
        return "Transactions"; 
      } 
      return null; 
     } 
    } 
} 
+0

而不是替換片段添加片段。 – nnn

+1

嘗試在除第一個片段之外的所有片段事務中添加'fragmentTransaction.addToBackStack(null)'。 –

+0

您的問題解決方案只是將所有片段添加到backStatck,然後在您的活動中覆蓋backPress以檢查backStackEntry –

回答

0

當你打電話,你必須添加任何片段。像下面一樣

mFragmentTransaction.addToBackStack("your_fregment_tag").commit(); 

當和後備。此代碼

@Override 
    public void onBackPressed() { 

     try { 
      int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount(); 

      if (mDrawerLayout.isDrawerOpen(GravityCompat.END)) { 
       mDrawerLayout.closeDrawer(GravityCompat.END); 
       return; 
      } else { 

       if (backStackEntryCount == 0) { // last fragment 
        android.app.AlertDialog.Builder alertDialog = new android.app.AlertDialog.Builder(DashboardActivity.this); 

        alertDialog.setTitle("Exit"); 
        alertDialog.setMessage("Are you sure you want to Exit?"); 

        alertDialog.setPositiveButton("YES", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int which) { 
            //finish(); 
            //System.exit(0); 
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
             finishAffinity(); 
            }else{ 
             finish(); 
             System.exit(0); 
            } 


           } 
          }); 

        alertDialog.setNegativeButton("NO", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int which) { 
            dialog.cancel(); 
           } 
          }); 

        alertDialog.show(); 
        return; 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 



     super.onBackPressed(); 
    } 

嘗試使用backStack。你必須觸發它。請檢查我的代碼

0

創建onbackpress功能時,您的主要活動 例如像這樣

@Override 
public void onBackPressed() { 
    if (count == 1) { 
     if (exit) { 
      super.onBackPressed(); 
      return; 
     } else { 
      Toast.makeText(this, "Press Back again to Exit.", Toast.LENGTH_SHORT).show(); 
      exit = true; 
      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        exit = false; 
       } 
      }, 2000); 
     } 
    }else { 
     toolbar.setTitle("Home"); 
     mDrawerLayout.closeDrawers(); 
     count=1; 
     display(0); 
    } 
} 

設置您的家庭片段的數量爲「1」,其他intger其他片段

0

更新您的NavigationItemSelectedListener有了這個。

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(MenuItem menuItem) { 
      mDrawerLayout.closeDrawers(); 
      if (menuItem.getItemId() == R.id.nav_item_income) { 
       FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); 
       IncomeFragment fragment = new IncomeFragment(); 
       String fragClassName = fragment.getClass().getName(); 
       fragmentTransaction.replace(R.id.containerView, fragment).addToBackStack(fragClassName).commit(); 
      } 

      if (menuItem.getItemId() == R.id.nav_item_expence) { 
       FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction(); 
       TabFragment fragment = new TabFragment(); 
       String fragClassName = fragment.getClass().getName(); 
       xfragmentTransaction.replace(R.id.containerView, fragment).addToBackStack(fragClassName).commit(); 
      } 
      return false; 
     } 

    }); 
1

首先添加您的片段在堆棧中:

if (menuItem.getItemId() == R.id.nav_item_income) { 
       FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); 
       fragmentTransaction.addToBackStack(null) //this line is needed 
       fragmentTransaction.replace(R.id.containerView,new IncomeFragment()).commit(); 

      } 

現在,在您的活動:覆蓋onBackPressed()方法

@Override 
public void onBackPressed() { 
    FragmentManager fm = getSupportFragmentManager(); 
     if (fm.getBackStackEntryCount() > 0) { 
      Log.i("MainActivity", "popping backstack"); 
      fm.popBackStack(); 
     } else { 
      Log.i("MainActivity", "nothing on backstack, calling super"); 
      new AlertDialog.Builder(this) 
       .setMessage("Are you sure you want to exit?") 
       .setCancelable(false) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         finishAffinity(); 
        } 
       }) 
       .setNegativeButton("No", null) 
       .show(); 
     } 
    }