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........