1. Are you ready for the Galaxy S20? Here is everything we know so far!

(Help) Pressing back on webview fragment activity does not go back to previous page

Discussion in 'Android Development' started by unet, Mar 11, 2021.

  1. unet

    unet Newbie
    Thread Starter

    Hello.
    I have an app that has fragment activities with webview and a navigational drawer. When I press back it goes to a previous fragment activity but it does not go back to the previous page on the webview.

    The code on MainActivity.java:
    Code (Java):
    1.  @Override
    2.     public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    3.         switch (item.getItemId()) {
    4.             case R.id.nav_facebook:
    5.                 getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
    6.                         new FacebookFragment()).addToBackStack("tag").commit();
    7.                 break;
    8.             case R.id.nav_twitter:
    9.                 getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
    10.                         new TwitterFragment()).addToBackStack("tag").commit();
    11.                 break;
    12.             case R.id.nav_instagram:
    13.                 getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
    14.                         new InstagramFragment()).addToBackStack("tag").commit();
    15.                 break;
    16.             case R.id.nav_linkedin:
    17.                 getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
    18.                         new LinkedinFragment()).addToBackStack("tag").commit();
    19.                 break;
    20.             case R.id.nav_snapchat:
    21.                 getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
    22.                         new SnapchatFragment()).addToBackStack("tag").commit();
    23.                 break;
    24.             case R.id.nav_pinterest:
    25.                 getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
    26.                         new PinterestFragment()).addToBackStack("tag").commit();
    27.                 break;
    28.             case R.id.nav_youtube:
    29.                 getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
    30.                         new YoutubeFragment()).addToBackStack("tag").commit();
    31.                 break;
    32.             case R.id.nav_about:
    33.                 getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
    34.                         new AboutFragment()).addToBackStack("tag").commit();
    35.                 break;
    36.             case R.id.nav_privacy:
    37.                 getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
    38.                         new PrivacyFragment()).addToBackStack("tag").commit();
    39.                 break;
    40.         }
    41.  
    42.         drawer.closeDrawer(GravityCompat.START);
    43.  
    44.         return true;
    45.     }
    46.  
    47.  
    48.     @Override
    49.     public void onBackPressed() {
    50.         FragmentManager fm = getSupportFragmentManager();
    51.         if (fm.getBackStackEntryCount() > 0) {
    52.             fm.popBackStack();
    53.         } else {
    54.             super.onBackPressed();
    55.         }
    56.     }
    57.  
    The code in one of the fragments (FacebookFragment.java):
    Code (Java):
    1. public class FacebookFragment extends Fragment {
    2.     public FacebookFragment() {
    3.  
    4.     }
    5.  
    6.     @Nullable
    7.     @Override
    8.     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    9.         View v = inflater.inflate(R.layout.fragment_facebook, container, false);
    10.         WebView webView = (WebView)v.findViewById(R.id.webView);
    11.         webView.getSettings().setJavaScriptEnabled(true);
    12.         webView.setWebViewClient(new WebViewClient());
    13.         webView.loadUrl("https://www.facebook.com/");
    14.         return v;
    15.  
    16.  
    17.  
    18.     }
    19.  
    20.  
    21.  
    22.  
    23.  
    24. }
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Help Pressing webview
  1. meditek
    Replies:
    14
    Views:
    412
  2. GiuseppeSM
    Replies:
    3
    Views:
    349
  3. Sreya Suresh
    Replies:
    1
    Views:
    238
  4. Rarena
    Replies:
    1
    Views:
    274
  5. shanks123
    Replies:
    0
    Views:
    305
  6. Laszlo Lebrun
    Replies:
    3
    Views:
    293
  7. spre248
    Replies:
    8
    Views:
    678
  8. cjnewman28
    Replies:
    2
    Views:
    576
  9. KenS999
    Replies:
    2
    Views:
    321
  10. xtianrogelio
    Replies:
    4
    Views:
    323

Share This Page

Loading...