2017-10-21 111 views
1

我在另一個片段中有3個片段,並且我在按鈕單擊之間切換片段。
我的問題是,當我按下後退按鈕,片段被隱藏,我需要再次按下後退按鈕退出片段。
如何解決這個問題?onBackPressed()在一個片段中

這是我的片段

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    if (rootView != null) { 
     ViewGroup parent = (ViewGroup) rootView.getParent(); 
     if (parent != null) 
      parent.removeView(rootView); 
    } 
    try { 
     rootView = inflater.inflate(R.layout.barber_descrption_layout_fragment, container, false); 
    } catch (InflateException e) { 
    } 
    context = getActivity(); 

    linearLayoutMain = (LinearLayout) rootView.findViewById(R.id.mainfrag); 
    myTextView_appointment = (MyTextView) rootView.findViewById(R.id.text_appi); 
    myTextView_map = (MyTextView) rootView.findViewById(R.id.textView2); 
    myTextView_images = (MyTextView) rootView.findViewById(R.id.img); 
    rate_btn = (LinearLayout) rootView.findViewById(R.id.rate_button); 
    rate_btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      ShowRateDialog(context); 
     } 
    }); 

    Fragment fragment = new Appointment_Fragment(); 
    Bundle bundle = new Bundle(); 
    fragment.setArguments(bundle); 
    getActivity().getSupportFragmentManager().beginTransaction() 
      .replace(R.id.mainfrag, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit(); 
    linearLayoutMain.setBackground(getResources().getDrawable(R.drawable.blue_boarder_map)); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 

    linearLayoutMain.setLayoutParams(params); 
    ////////////////////////////////// 
    myTextView_appointment.setBackground(getResources().getDrawable(R.drawable.appi_color)); 
    myTextView_appointment.setTextColor(getResources().getColor(R.color.white)); 
    ///////////////////////// 
    myTextView_appointment.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      myTextView_appointment.setBackground(getResources().getDrawable(R.drawable.appi_color)); 
      myTextView_appointment.setTextColor(getResources().getColor(R.color.white)); 
      myTextView_map.setTextColor(getResources().getColor(R.color.blue_color)); 
      myTextView_map.setBackground(getResources().getDrawable(R.drawable.map_color_white)); 
      myTextView_images.setTextColor(getResources().getColor(R.color.blue_color)); 
      myTextView_images.setBackground(getResources().getDrawable(R.drawable.blue__white)); 
      linearLayoutMain.setBackgroundColor(getResources().getColor(R.color.white)); 

      Fragment fragment = new Appointment_Fragment(); 
      Bundle bundle = new Bundle(); 
      fragment.setArguments(bundle); 
      getActivity().getSupportFragmentManager().beginTransaction() 
        .replace(R.id.mainfrag, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit(); 
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
      params.setMargins(0, 0, 0, 0); 
      linearLayoutMain.setLayoutParams(params); 

     } 
    }); 
    myTextView_map.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      myTextView_map.setBackground(getResources().getDrawable(R.drawable.map_color)); 
      myTextView_map.setTextColor(getResources().getColor(R.color.white)); 
      myTextView_appointment.setTextColor(getResources().getColor(R.color.blue_color)); 
      myTextView_appointment.setBackground(getResources().getDrawable(R.drawable.apii_color_white)); 
      myTextView_images.setTextColor(getResources().getColor(R.color.blue_color)); 
      myTextView_images.setBackground(getResources().getDrawable(R.drawable.blue__white)); 
      linearLayoutMain.setBackground(getResources().getDrawable(R.drawable.blue_boarder_map)); 
      Fragment fragment = new MapFragment_Barber(); 
      Bundle bundle = new Bundle(); 
      fragment.setArguments(bundle); 
      getActivity().getSupportFragmentManager().beginTransaction() 
        .replace(R.id.mainfrag, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit(); 
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
      params.setMargins(40, 40, 40, 40); 
      linearLayoutMain.setLayoutParams(params); 

     } 
    }); 

    myTextView_images.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      myTextView_images.setBackgroundColor(getResources().getColor(R.color.blue_color)); 
      myTextView_images.setTextColor(getResources().getColor(R.color.white)); 
      myTextView_map.setTextColor(getResources().getColor(R.color.blue_color)); 
      myTextView_map.setBackground(getResources().getDrawable(R.drawable.map_color_white)); 

      myTextView_appointment.setTextColor(getResources().getColor(R.color.blue_color)); 
      myTextView_appointment.setBackground(getResources().getDrawable(R.drawable.apii_color_white)); 
      linearLayoutMain.setBackgroundColor(getResources().getColor(R.color.white)); 
      Fragment fragment = new BarberImage(); 
      Bundle bundle = new Bundle(); 
      fragment.setArguments(bundle); 
      getActivity().getSupportFragmentManager().beginTransaction() 
        .replace(R.id.mainfrag, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit(); 

      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
      params.setMargins(40, 40, 40, 0); 
      linearLayoutMain.setLayoutParams(params); 
     } 
    }); 

    return rootView; 
} 

我需要這樣,當我按下後退按鈕我在第一次點擊withiut需要多次點擊的退出片段。

回答

0

請按照此。它通過使用Abstract ClassFragment上實現onBackPressed()功能的絕佳示例。

1)創建一個抽象類 -

import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 

public abstract class BackableFragment extends Fragment implements View.OnKeyListener { 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     view.setFocusableInTouchMode(true); 
     view.requestFocus(); 
     view.setOnKeyListener(this); 
    } 

    @Override 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
     if (event.getAction() == KeyEvent.ACTION_UP) { 
      if (keyCode == KeyEvent.KEYCODE_BACK) { 
       onBackButtonPressed(); 
       return true; 
      } 
     } 

     return false; 
    } 

    public abstract void onBackButtonPressed(); 
} 

2)實施Abstract ClassFragment -

public class Appointment_Fragment extends BackableFragment { 

public static final String CLASS_NAME = "Appointment_Fragment"; 
public FragmentChangeListener fragmentChangeListener; 

    ... 

    @Override 
    public void onBackButtonPressed() { 
     fragmentChangeListener.OnFragmentChange(CLASS_NAME); 
    } 

    ... 
} 

3)作出interface與名稱FragmentChangeListener

interface FragmentChangeListener{ 
    public void onFragmentChange(String fragmentName); 
} 

4)實現它在你的activity -

public class HomeActivity extends AppCompatActivity implements FragmentChangeListener{ 
........ 
....... 
....... 

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

      Fragment fragment = new Appointment_Fragment(); 
      fragment.fragmentChangeListener = this; 
     Bundle bundle = new Bundle(); 
     fragment.setArguments(bundle); 
     getActivity().getSupportFragmentManager().beginTransaction() 
       .replace(R.id.mainfrag, fragment).commit(); //Always use getChildFragmentManager() for child fragment 
     } 

@Override 
    public void onFragmentChange(String fragmentName){ 
     if(fragmentName.equals("Appointment_Fragment")){ 
      //do what do you want 
     }else if(fragmentName.equals("FragmentB")){ 
      // do what do you want 
     }else if(fragmentName.equals("FragmentC")){ 
      //do what do you want 
     } 
    } 

} 

完蛋了。 Reference

+0

同樣的問題!它不適合我! –

+0

讓你的父母'Frgament'成爲'BackableFragment',你的3個孩子片段的容器就放在這個'BackableFragment'上。 –

+0

假設你有多個片段。在所有片段中調用Activity.onBackPressed都會導致在任何片段中按Back鍵時活動結束。我建議在片段中設置一個OnBackPressedListener,並讓Activity在特定片段報告OnBackPressed時決定要做什麼。 – Christine

0

如果你在片斷,然後去上onBackPress childFragmentManager

android.support.v4.app.FragmentManager fm = getChildFragmentManager(); 

殺了父片段

+0

可以解釋更多!我不明白這個「onBackPress只是殺死父母的片段」 –

+0

@MoayedAlayaseh你的活動必須有一個父母片段的FragmentManager。在onBackpress的片段調用後臺活動,並檢查片段管理器中是否存在片段,然後從堆棧中彈出片段。所以,如果父母死亡。子片段會自動殺死。 –

+0

它不工作,,,但我在主要活動「android.support.v4.app.FragmentManager fm = getSupportFragmentManager();」因爲getChildFragmentManager();它不是在活動,而同樣的問題不工作! –