Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App closed after click on back button in Fragment

[UPDATE]

Problem solved : Simply add "addToBackStack(null)" before commit fragment :

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

Here is the code of my fragment. When i am on this fragment and i click on back button, my application is closed.

I would like to go back on the previous loaded fragment.

What can i do to force this behavior ?

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;
    }
like image 272
wawanopoulos Avatar asked Oct 21 '22 04:10

wawanopoulos


1 Answers

You have to use FragmentTransaction.addToBackStack method when attaching your fragment to Activity. Here is the quote from documentation

Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

like image 185
Desert Avatar answered Oct 24 '22 09:10

Desert