• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Make sharedPrefrences data a string

cr5315

Android Enthusiast
I have an app that uses sharedPreferences to send a link from one activity to the next. What I can't figure out is how I would take that link and insert it into a string.

Here's the code I've got:

First.class
Code:
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
     {
    	 Log.i(tag,"item clicked! [" + RSSFeed.getItem(position).getTitle() + "]");
    	 
    	 // Get the app's shared preferences
    	 SharedPreferences app_preferences = 
         PreferenceManager.getDefaultSharedPreferences(this);
    	 app_preferences.getInt("link", 0);
    	 
    	 Intent itemintent = new Intent(this,ArticleView.class);
    	 
    	 SharedPreferences.Editor editor = app_preferences.edit();
         editor.putInt(RSSFeed.getItem(position).getLink(), position);
         editor.commit(); // Very important
         
         startActivity(itemintent);
     }

Second.class
Code:
public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        // Get the app's shared preferences
   	    SharedPreferences app_preferences = 
        PreferenceManager.getDefaultSharedPreferences(this);
   	    app_preferences.getInt("link", 0);
   	    final String BLARP = ("yes"); 
        
        webview = (WebView) findViewById(R.id.webview);
        
        webview.getSettings().setJavaScriptEnabled(true);
        
        webview.loadUrl(BLARP);
        
        webview.setWebViewClient(new HelloWebViewClient());
        
    }

What I want is to get the "link" from the sharedPreferences into the "yes" from the string. Any ideas on how I could do that?
 
You can use SharedPreferences if you want, but just remember that the SharedPreferences will stay around (with the code you have) until the user uninstalls your app.

An easier and better way would be to pass the link through the intent which starts the new activity.

For instance, in activity A you could start Activity B with the following code:

Code:
Intent intent = new Intent(this, com.YourDomain.ActivityB.class);
intent.putExtra("link", "http://www.google.com");
startActivity(intent);

then, in Activity B you could collect that data by using the following code:

Code:
Intent intent = getIntent();
String link = intent.getStringExtra("link");

intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
startActivity(intent);

Boogs
 
Back
Top Bottom