Android:在两个活动之间切换并在它们之间共享信息

发布于 2024-12-11 01:57:14 字数 185 浏览 1 评论 0原文

这是一个基本问题,但我需要一些帮助。

我有两项活动:actA、actB。 在 actA 中,我想启动 actB 并给它一个字符串,而不是想结束 actB 并将另一个字符串返回给 actA (我不想转到 actA 的 onCreate() ,我宁愿将此值返回给某个 ,以便它可以使用 actB 中的字符串。

actA 中的方法

This is a basic question but I need some help with it.

I have two activities : actA, actB.
While in actA I want to start actB and give it a String, than I want to end actB and return another String to actA (I don't want to go to onCreate() of actA, I would much rather return this value to some method in actA so it can use the String from actB.

Help is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半边脸i 2024-12-18 01:57:14
From A.java:
Intent myintentB=new Intent(A.this, B.class).putExtra("<StringName>", "Value");
    startActivityForResult(myintentB, 3);

    from B.java:

    Intent myintentA=new Intent(B.this, A.class).putExtra("<StringName>", "Value"); 
    finish();
    setResult(3, myintentA);


    In A.java
@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
            String result_string=data.getStringExtra("<StringName>");
        }
From A.java:
Intent myintentB=new Intent(A.this, B.class).putExtra("<StringName>", "Value");
    startActivityForResult(myintentB, 3);

    from B.java:

    Intent myintentA=new Intent(B.this, A.class).putExtra("<StringName>", "Value"); 
    finish();
    setResult(3, myintentA);


    In A.java
@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
            String result_string=data.getStringExtra("<StringName>");
        }
只有一腔孤勇 2024-12-18 01:57:14

在活动 A 中:

Intent intent = new Intent();
intent.setClass (getApplicationContext(), ActB.class) ;
intent.putExtra ("data1","NEW STRING") ;
context.startActivityForResult(intent) ;

在活动 B 中(onCreate 方法):

Intent intent = getIntent() ;
if (intent.hasExtra("data1") )
String dataSent = intent.getStringExtra("data1") ;

发送回数据时:

Intent intent = new Intent() ;
intent.putExtra ("Return" , "RETURN STRING") ;
setResult(RESULT_OK, intent) ;
finish() ;

在活动 A 中: (onActivityResult) [ 您需要重写]

if (data.hasExtra("Return"))
String data1 = data.getStringExtra("Return");

In Activity A :

Intent intent = new Intent();
intent.setClass (getApplicationContext(), ActB.class) ;
intent.putExtra ("data1","NEW STRING") ;
context.startActivityForResult(intent) ;

In Activity B (onCreate Method) :

Intent intent = getIntent() ;
if (intent.hasExtra("data1") )
String dataSent = intent.getStringExtra("data1") ;

While sending data back :

Intent intent = new Intent() ;
intent.putExtra ("Return" , "RETURN STRING") ;
setResult(RESULT_OK, intent) ;
finish() ;

In Activity A : (onActivityResult) [ You need to override ]

if (data.hasExtra("Return"))
String data1 = data.getStringExtra("Return");
神经大条 2024-12-18 01:57:14

下面链接的问题的答案也描述了您正在寻找的相同问题

如何在 onResume() 中从包中提取字符串?

the answer to question linked below also describes the same issue you are looking for

How to pull string from bundle in onResume()?

不必了 2024-12-18 01:57:14

对于您的第一个问题,您可以在实际创建 Intent 之前使用方法 intentname.putExtra("extravalue", value); 为新 Intent 提供额外的值。
然后,您可以在新创建的活动中读取该值。我知道我不擅长解释,但我希望您能从我制作的应用程序中获取的这段代码中得到启发。

ListLinks.java:LinkView.java

            // Pass the value of the item URL to the linkviewer when a link is clicked
        Intent openLink = new Intent(this, LinkView.class);
        openLink.putExtra("url" , item.URL);
        startActivity(openLink);

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.linkview);

    Bundle bun = getIntent().getExtras();
    String url = bun.getString("url");

我不太确定如何解决你的第二个问题,所以遗憾的是我无法帮助你解决这个问题,但我相信其他人可以

As for your first problem, you can give extra values to a new Intent by using the method intentname.putExtra("extravalue", value); before actually creating the Intent.
You can then read that value out in the the newly created Activity. I know I'm pretty terrible at explaining, but I hope you get the idea from this piece of code I took from an app I made.

ListLinks.java:

            // Pass the value of the item URL to the linkviewer when a link is clicked
        Intent openLink = new Intent(this, LinkView.class);
        openLink.putExtra("url" , item.URL);
        startActivity(openLink);

LinkView.java

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.linkview);

    Bundle bun = getIntent().getExtras();
    String url = bun.getString("url");

I'm not quite sure about how to resolve your second problem, so sadly I cannot help you with this one but I'm sure someone else might.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文