Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8329763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:54:11+00:00 2026-06-09T01:54:11+00:00

I have a fragment class. Here is it below: public class FragmentA extends Fragment

  • 0

I have a fragment class. Here is it below:

public class FragmentA extends Fragment {

Button button;
WebView myWebView;
int mCurCheckPosition;

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

 }

 @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }

 @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
            // Restore last state for checked position.
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
     View mainView = (View) inflater.inflate(R.layout.frag_a, group, false);
     myWebView = (WebView) mainView.findViewById(R.id.webview);
    myWebView.setWebViewClient(new MyWebViewClient());
    myWebView.getSettings().setPluginsEnabled(true);
    myWebView.getSettings().setBuiltInZoomControls(false); 
    myWebView.getSettings().setSupportZoom(false);
    myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);   
    myWebView.getSettings().setAllowFileAccess(true); 
    myWebView.getSettings().setDomStorageEnabled(true);
    myWebView.loadUrl("http://www.bbc.co.uk");       
    return mainView;
}


public class MyWebViewClient extends WebViewClient {        
    /* (non-Java doc)
     * @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
     */


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.endsWith(".mp4")) 
        {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), "video/*");

            view.getContext().startActivity(intent);
            return true;
        } 
        else {
            return super.shouldOverrideUrlLoading(view, url);
        }
    }

}

}

The problem is when I move to and from another fragment, the state of the original fragment (what web page it was on) is lost.

How can I prevent this? I want the state of the web page to remain switching to and from each fragment.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:54:14+00:00Added an answer on June 9, 2026 at 1:54 am

    You should use the WebView.saveState(Bundle state) method to in your onSaveInstanceState(Bundle outState) method and then restore the state using WebView.restoreState(Bundle state) in your onActivityCreated(Bundle savedInstanceState) method

    @Override
    public void onSaveInstanceState(Bundle outState) {
       super.onSaveInstanceState(outState);
       mWebView.saveState(outState);
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
       super.onActivityCreated(savedInstanceState);
       mWebView.restoreState(savedInstanceState);
    }
    

    Also keep in mind the Fragment Lifecycle. If your unsure where to restore the state (onCreate, onCreateView, onActivityCreated), take a look at the Fragment Lifecycle documentation to figure out the right place.
    http://developer.android.com/guide/components/fragments.html

    enter image description here

    onCreate()

    The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

    onCreateView()

    The system calls this when it’s time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment’s layout. You can return null if the fragment does not provide a UI.

    onActivityCreated()

    Called when the fragment’s activity has been created and this fragment’s view hierarchy instantiated. It can be used to do final initialization once these pieces are in place, such as retrieving views or restoring state. It is also useful for fragments that use setRetainInstance(boolean) to retain their instance, as this callback tells the fragment when it is fully associated with the new activity instance. This is called after onCreateView(LayoutInflater, ViewGroup, Bundle) and before onStart().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a BaseFragment so called LeaderboardFragment that extends Fragment : public class LeaderboardFragment
Suppose I have several fragments : public class FirstFragment extends Fragment { ... }
I have a very simple activity just to show the preference fragment: public class
I have a custom ArrayAdapter populating a ListView . Here it is: public class
I hava a fragment named DetailFragment which extends Fragment class with neccessary Override methods:
I have successfully implemented actionsherlock in my project.so here is my code: public class
I have class that extend FragmentActivity in it I add fragment to layout as
I'm developing a ListView in an Android Fragment. The class extends ListFragment. I tried
Below I have put a fragment of code to help understand my problem. I
I have a DialogFragment defined as inner class in my Fragment class. On Orientation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.