如何获得开始活动的结果从第 4 个活动获取结果



我从该startActivityForResult()中获得了第 2 activtiy MainActivity,但我希望从第 4 activity 获得结果,我该怎么办?

主活动

第一活动

第二活动

第三活动

四大活动

由此,我希望结果来自第 4 个活动,但从第二个活动到第 4 个活动是依赖活动

主活动.java

public static String MY_RESULT=1000;
     intent=new Intent(MainActivity.this,FirstActivity.class);
                    startActivityForResult(intent,MY_RESULT);


 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case MY_RESULT:
                    String info = data.getExtras().getString("KEY");

                    if(!info.equals("")) {
                        Log.i("TAAAG", info);
                    }
                    break;
}
}
}

第一活动.java

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                    startActivity(intent);
finish();

第二活动.java

Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
                    startActivity(intent);
finish();

第三活动.java

 Intent intent = new Intent(ThirdActivity.this, FourActivity.class);
                        startActivity(intent);
finish();

四活动.java

  Intent intent = new Intent();

         intent.putExtra("KEY", "my data in string");
                        setResult(RESULT_OK, intent);

      finish();

MainActivity.java这里,我无法得到任何结果。我希望从FourActivity.java中得到结果.请帮忙

没有简单的方法,您必须将startActivityForResult()用于第一到第三个活动,并使用setResult()来侦听和传回数据。

下面是示例代码:

class FirstActivity extends Activity {
    public static int MY_RESULT = 1000;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(FirstActivity.this, FirstActivity.class);
        startActivityForResult(intent, MY_RESULT);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MY_RESULT && resultCode == RESULT_OK) {
            String dataStr = data.getStringExtra("KEY");
            Toast.makeText(this, dataStr, Toast.LENGTH_LONG).show();
        }
    }
}
class SecondActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
        startActivityForResult(intent, FirstActivity.MY_RESULT);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == FirstActivity.MY_RESULT && resultCode == RESULT_OK) {
            setResult(resultCode, data);
            finish();
        }
    }
}
class ThirdActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(ThirdActivity.this, FourthActivity.class);
        startActivityForResult(intent, FirstActivity.MY_RESULT);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == FirstActivity.MY_RESULT && resultCode == RESULT_OK) {
            setResult(resultCode, data);
            finish();
        }
    }
}
class FourthActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent data = new Intent();
        data.putExtra("KEY", "This is a test String");
        setResult(RESULT_OK, data);
        finish();
    }
}

其他可能的解决方案包括:

  1. 定义您自己的Singleton,可以在应用程序会话期间保存数据
  2. 使用EventBus
  3. 使用SharedPreferences
  4. 使用本地数据库

您可以启动结果的每个活动并像这样传递结果:

FourthActivity -> ThirdActivity -> SecondActivity -> FirstActivity

在您的第三、第二和第一次活动中,使用 onActivityResult 从先前关闭的Activity中获取结果。

在第四个活动中,您在setResult()中设置了RESULT_OK,这很好,但在您的onActivityResult中只需删除它:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
                String info = data.getExtras().getString("KEY");
                if(!info.equals("")) {
                    Log.i("TAAAG", info);
  }
}

快乐编码!!

相关内容

  • 没有找到相关文章

最新更新