-2

I am a new to java and android, actually i am stuck in a problem, before down-voting or marking it as a duplicate , try to help me to solve this storm...

I will describe my concept. I have a web-view fragment in that i have a button . when ever the user click the button while browsing , the current web address will pass from fragment to main activity by using interface like this. i want to make that passed web addresses as the default address when the next time user open the app(it should load that web address which is passed from fragment last time).

this is how is how i am passing the web address to URL from fragment to main activitY

  @Override
public void onHomeClick(String pasedurl) {

    // now URL having the web address
    URL= pasedurl;

//trying to save URL for the next time use

final String MY_PREFS_NAME = "MyPrefsFile";
     SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
    editor.putString("name", URL);
    editor.commit();


    SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    String restoredText = prefs.getString("name", null);
    if (restoredText == null) {


        String name = prefs.getString("name", "No name defined");

        Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();

    }

The next step i took is passing a url string from main activity to web fragment so that next time it will open the web address which is saved before

this is how i am passing string url from main activity to fragment

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState)

   //passing the sting 'name'

    Bundle bundle=new Bundle();
    bundle.putString("url", name);
    final Radio radio=new Radio();
    radio.setArguments(bundle);


    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


   //other stuff

    } 

and i am receiving in web-fragment like this`

final WebView view=(WebView) rootView.findViewById(R.id.webView);
    String url = getArguments().getString("url");
    view.loadUrl(url);

So now i will tell you what i want . I want to pass the saved string url from shared preference to the bundle.so web fragment can catch it,or i want to make toast from main activity like

 Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();

so i will get to know the last time saved url, how can i do that ? please help me........

Community
  • 1
  • 1
Jaison Joseph
  • 79
  • 1
  • 2
  • 10

2 Answers2

2

sharedpreferences also can be access in fragment

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.your_fragment, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
     super.onViewCreated(view, savedInstanceState);
    final String MY_PREFS_NAME = "MyPrefsFile";
     SharedPreferences shared = getActivity().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
     String url = shared.getString("name",null); 

     final WebView view=(WebView) view.findViewById(R.id.webView);
     view.loadUrl(url);

}
ZeroOne
  • 8,996
  • 4
  • 27
  • 45
1

Sharedpreferense will what the data through the intire app so you can just add in the sharedpreferense with the same reference and regrab the string variable. So just put this line where you want to grab the data again: SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); String restoredText = prefs.getString("name", null);