简体   繁体   English

如何将网址从一个班级转移到另一个活动

[英]How to transfer url from one class to another activity

In my app urls are getting from the firebase database .在我的应用程序中,url 来自 firebase 数据库。 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.我如何将 url(Uri 网页)传递给查看器类。请告诉我该怎么做。

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

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.在发件人的活动中,您可以将 URL 字符串数组添加到意图中。

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.在 MainActivity (Receiver) 中,您可以从 Intent 获取 String 数组。

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.希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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