简体   繁体   中英

How to transfer url from one class to another activity

In my app urls are getting from the firebase database . I want to send these url to the another activity. how can i pass the url (Uri webpage) to the viewer class.Please tell me how to do it.

Use intent in first activity like this:

Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("key", value);
CurrentActivity.this.startActivity(intent);

And retrieve it on the second activity:

@Override
 protected void onCreate(Bundle savedInstanceState) {
 Intent intent = getIntent();
 String value = intent.getStringExtra("key");
}

you can use this SharedPreferences

SharedPreferences sharedPref = getSharedPreferences("key",Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("key1","your uri");
    editor.apply();

if you want get value in new ativity

SharedPreferences sharedPref = getSharedPreferences("key",Context.MODE_PRIVATE);
String uri=sharedPref.getString("key1","default value if value is null");

keys values must be same

You can also send String Array via intent from one activity to another.

In sender's activity, you can add URL string array into intent.

Intent intent = new Intent(this, MainActivity.class);
String[] urls = new String[] {
        "https://google.com",
        "https://stackoverflow.com"
};
intent.putExtra("urls", urls);
startActivity(intent);

In MainActivity (Receiver), you can get String array from intent.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] urls = getIntent().getStringArrayExtra("urls");
    }
}

Hope it will be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM