如何获得startactivityforresult从第4个活动获得结果

时间:2017-11-09 10:27:06

标签: android startactivityforresult

我从第MainActivity startActivityForResult() activtiy获得了activity,但我希望第4次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; } } } 的结果能为我做些什么?

  

MainActivity

     

FirstActivity

     

secondActivity

     

ThirdActivity

     

FourActivity

从这里我想要第4次活动的结果,但是从第2次活动到第4次活动是依赖活动

MainActivity.java

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

FirstActivity.java

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

SecondActivity.java

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

ThirdActivity.java

  Intent intent = new Intent();


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

                        setResult(RESULT_OK, intent);


      finish();

FourActivity.java

MainActivity.java

此处FourActivity.java我无法获得任何结果。我想要section .data text1 db "Enter the first number :" text2 db "Enter the second number :" section .bss num1 resb 2 num2 resb 2 sum_temp resb 4 section .text global _start _start: ; print : "Enter the first number: " mov rax, 1 mov rdi, 1 mov rsi, text1 mov rdx, 24 syscall ; get the first number from input mov rax, 0 mov rdi, 0 mov rsi, num1 mov rdx, 2 syscall ; print: "Enter the second number: " mov rax, 1 mov rdi, 1 mov rsi, text2 mov rdx, 25 syscall ; get the second number from input mov rax, 0 mov rdi, 0 mov rsi, num2 mov rdx, 2 syscall ; load two numbers into registers for addition mov rbx, [num1] mov rcx, [num2] ; add the two number and save the result in rbx add rbx, rcx ; show the result to the user mov [sum_temp], rbx mov rax, 1 mov rdi, 1 mov rsi, sum_temp mov rdx, 4 syscall mov rax, 60 mov rdi, 0 syscall 的结果。请帮助

3 个答案:

答案 0 :(得分:2)

没有简单的方法,您必须使用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,可以在App Session
  2. 期间保存数据
  3. 使用EventBus
  4. 使用SharedPreferences
  5. 使用本地数据库

答案 1 :(得分:0)

您可以为结果启动每个活动并传递结果,如下所示:

FourthActivity -> ThirdActivity -> SecondActivity -> FirstActivity

在您的第三,第二和第一个活动中,使用onActivityResult来获取之前关闭的Activity的结果。

答案 2 :(得分:0)

在第四项活动中,您在RESULT_OK中设置setResult()即可,但在您的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);

  }
}

快乐的编码!!