2017-12-22 19 views
0

を設定しました。最初のタブにがあります。ボタンをクリックすると別のに行きます。しかし、戻るボタンを押してに戻ると、私のレイアウトは消え、白い画面が表示されます。私は新しいフラグメントに行くとレイアウトが消え、画面が白表示されている再びに来るとき これはタブの一つフラグメントから戻るとTabLayoutで白い画面が表示される

public class CurrentStatus extends Fragment { 

    // 
    // FragmentManager fm; 
    FragmentManager fm; 
    FragmentTransaction tx; 

    RecyclerView recyclerView; 
    // RecyclerView.Adapter adapter; 
    NewAdapter adapt; 
    private ArrayList<CurrentEntry> current; 
    String userUrl; 
    String second_req="second_req"; 
    EditText search; 

    public CurrentStatus() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 

     View v=inflater.inflate(R.layout.current_status,container,false); 
     FloatingActionButton fab=(FloatingActionButton)v.findViewById(R.id.fab); 
     recyclerView=(RecyclerView)v.findViewById(R.id.recyclerView); 
     recyclerView.setHasFixedSize(true); 
     recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
     current=new ArrayList<>(); 
     search=(EditText)v.findViewById(R.id.search); 


     getData(); 

     search.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
        filter(s.toString()); 
      } 
     }); 
     fm=getFragmentManager(); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String tag="one"; 
       tx=fm.beginTransaction(); 
       tx.replace(R.id.frame,new Fragment(),tag); 
       tx.addToBackStack(tag); 
       tx.commit(); 
      } 
     }); 


     return v; 


    } 

ため

public class ChooseTab extends Fragment implements TabLayout.OnTabSelectedListener{ 

    TabLayout tabLayout; 
    private ViewPager viewPager; 
    RelativeLayout relativeLayout; 
    FragmentManager fm; 
    FragmentTransaction tx; 
    String back=""; 

    public ChooseTab() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v=inflater.inflate(R.layout.choose_tab,container,false); 
     relativeLayout=(RelativeLayout)v.findViewById(R.id.relative); 
     tabLayout=(TabLayout)v.findViewById(R.id.simpleTabLayout); 
     viewPager=(ViewPager)v.findViewById(R.id.simpleViewPager); 
     viewPager.setOffscreenPageLimit(3); 
     tabLayout.addTab(tabLayout.newTab().setText("Tab 1")); 
     tabLayout.addTab(tabLayout.newTab().setText("Tab 2")); 
     tabLayout.addTab(tabLayout.newTab().setText("Tab 3")); 

     PagerAdapter adapter=new PagerAdapter(getActivity().getSupportFragmentManager(),tabLayout.getTabCount()); 
     viewPager.setAdapter(adapter); 
     tabLayout.addOnTabSelectedListener(this); 
     fm=getFragmentManager(); 
     tx=fm.beginTransaction(); 
     tx.addToBackStack(back); 
     tx.commit(); 


     viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
     return v; 
    } 


    @Override 
    public void onTabSelected(TabLayout.Tab tab) { 
     viewPager.setCurrentItem(tab.getPosition()); 
    } 

    @Override 
    public void onTabUnselected(TabLayout.Tab tab) { 

    } 

    @Override 
    public void onTabReselected(TabLayout.Tab tab) { 
     viewPager.setCurrentItem(tab.getPosition()); 
    } 



} 

これでコードのためのコードです。

これはあなたの活動のMainActivity

ためのコード
public void onBackPressed() { 
     Fragment f=getSupportFragmentManager().findFragmentById(R.id.frame); 
     if(f instanceof Choose) 
     { 
      if (doubleBackToExitPressedOnce) 
      { 
       super.onBackPressed(); 
       return; 
      } 

      doubleBackToExitPressedOnce = true; 
      Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show(); 

      new Handler().postDelayed(new Runnable() { 

       @Override 
       public void run() { 
        doubleBackToExitPressedOnce=false; 
       } 
      }, 2000); 
     } 
     else { 
      super.onBackPressed(); 
     } 

    } 
+0

「私が新しい断片に行くとき」は、バックプレスコードはどこですか?ポストバックプレスコード。 – ADM

+0

フラグメント内のonBackPressedをオーバーライドすることができません –

+0

MainActivityにこのコードを追加しました –

答えて

0

オーバーライドonBackPressed()です。問題のリロードを乗り越えるために代わりreplaceの新しいフラグメントの使用addを追加するために

@Override 
public void onBackPressed() { 
    if (getSupportFragmentManager().getBackStackEntryCount() == 0) { 
     finish(); 
    } else { 
     getSupportFragmentManager().popBackStackImmediate(); 
    } 
} 

。フラグメント内にネストされたフラグメントがある場合は、getChildFragmentManager()のトランザクションを使用し、アクティビティのonBackPressed()の親フラグメントのgetChildFragmentManager()でそれを検証する必要があります。

+0

コードをダブルクリックして終了します。終了するには –

+0

ダブルタブを終了しますか? 'if'の中で最初の条件で行うことができます – ADM

+0

置き換える必要のある新しいフラグメントを追加している間。 MainActivityまたは現在のタブフラグメント –

関連する問題