2013-07-22 15 views
5

[UPDATE]App chiuso dopo click sul pulsante Indietro nel Frammento

Problema risolto: basta aggiungere "addToBackStack (null)" prima di commettere frammento:

Fragment fragment = new WebBrowserFragment(); 
fragment.setArguments(arguments); 
FragmentTransaction fragmentTransaction = getActivity().getFragmentManager().beginTransaction(); 
fragmentTransaction.replace(R.id.content_frame, fragment); 
fragmentTransaction.addToBackStack(null); 
fragmentTransaction.commit(); 

Ecco il codice del mio frammento. Quando sono su questo frammento e faccio clic sul pulsante Indietro, la mia domanda è chiusa.

Mi piacerebbe tornare sul frammento caricato precedente.

Cosa posso fare per forzare questo comportamento?

public class WebBrowserFragment extends Fragment { 

    WebView mWebView; 
    ProgressBar progressB = null; 
    private String mUrl; 
    private static String mtitle; 
    private static String msource; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     LayoutInflater mInflater = (LayoutInflater) getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     View view = (View) mInflater.inflate(R.layout.web_browser_view, null); 

     MainActivity.setShareButtonToVisible(); 

     Bundle bundle = getArguments(); 
     String url = bundle.getString("URL"); 
     mtitle = bundle.getString("TITRE"); 
     msource = bundle.getString("SOURCE"); 

     mUrl = url; 

     progressB = (ProgressBar) view.findViewById(R.id.progressBar1); 

     mWebView = (WebView) view.findViewById(R.id.webViewArticle); 
     mWebView.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) { 
       if(progress < 100 && progressB.getVisibility() == ProgressBar.GONE){ 
        progressB.setVisibility(ProgressBar.VISIBLE); 
       } 
       progressB.setProgress(progress); 
       if(progress == 100) { 
        progressB.setVisibility(ProgressBar.GONE); 
       } 
      } 
     }); 
     mWebView.loadUrl(url); 
     return view; 
    } 
+0

Invia il logcat. –

risposta

2

si deve utilizzare FragmentTransaction.addToBackStack metodo quando allegando il tuo frammento di attività. Ecco la citazione dalla documentazione

Aggiungere questa transazione allo stack posteriore. Ciò significa che la transazione verrà ricordata dopo che è stata commessa, e invertirà la sua operazione quando in seguito sarà saltata fuori dallo stack.

+0

Devo richiamarlo prima di 'fragmentManager.beginTransaction(). Replace (R.id.content_frame, fragment). Com();'? – wawanopoulos

+1

Risolto! Vedi il mio post – wawanopoulos

+0

sì, è corretto :) – Desert

Problemi correlati