活动 - 未调用onActivityResult

时间:2018-03-19 15:27:11

标签: android android-intent

我有三项活动:a1 - > a2 - > A3

a1是一种加载数据的介绍,最后使用startActivity(a2)来调用a2。 a2使用带有a2.this.startActivityForResult(intentForA3, 1);的FloatingActionButton调用a3 a2覆盖onActivityResult - 方法,但是当a3关闭时,永远不会被调用   setResult(RESULT_OK, returnIntent); finish();

a2正在使用带有适配器的RecyclerView,但Button直接放在a2中,所以这应该不是问题。但是关闭a3可以最大限度地减少所有活动。它仍在后台运行并恢复显示a1,quickley“跳过”到a2。

这是我的清单:

    <activity android:name="de.activity.A1"
        android:label="">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="de.activity.A2"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>

    <activity android:name="de.activity.A3"
        android:launchMode="singleTop"
        android:parentActivityName="de.activity.A2"
        android:label="">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="de.activity.A2" />
    </activity>

我的配置:

    compileSdkVersion 24
    buildToolsVersion "25.0.0"
    defaultConfig {
       applicationId "something here"
       minSdkVersion 15
       targetSdkVersion 24
       versionCode 1
       versionName "1.0"
    }

修改

A2:

    public class A2 extends AppCompatActivity {
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent insertIntent = new Intent(getApplicationContext(), A3.class);
                    Bundle b = new Bundle();
                    b.putSerializable("content", importantContent);
                    insertIntent.putExtra("content", b);
                    A2.this.startActivityForResult(insertIntent, 1);
                }
            });

       @Override
       public void onActivityResult(int requestCode, int resultCode, Intent data) {
             // Never called
             if (requestCode == 1) {
                   if (resultCode == Activity.RESULT_OK) {
                          switch (data.getAction()) {
                             case CMD_SAVE_OBJECT:
                             break;
                          }
                   }
             }
       }
  }

A3:

    public class A3 extends AppCompatActivity {

       // ...

       @Override
       public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.

            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_save_object) {
                readTopicFromView();

                Intent returnIntent = new Intent(CMD_SAVE_OBJECT);
                setResult(RESULT_OK, returnIntent);
                finish();
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }

另一个编辑:

当我按下a3工具栏中的“后退”箭头时,我遇到了同样的问题。是否存在PARENT_ACTIVITY - 清单中的问题,将返回转发给第一个活动?

3 个答案:

答案 0 :(得分:0)

在活动3的设定结果中使用此代码

 Intent intent = new Intent();
 intent.putExtra("MESSAGE", "Your message to pass to parent actvity"));
 setResult(1, intent);
 finish();

答案 1 :(得分:0)

您试图从意图数据中获取操作,而您还没有将其放在 a3 的意图中。尝试通过 resultIntent.putExtra(&#34; ACTION&#34;,&#34; my_action&#34;)将其放入 resultIntent ,然后将其发送到a2活动并通过 onActivityResult()

中的a2从 data.getStringExtra(&#34;键&#34;)获取

答案 2 :(得分:0)

这是因为您使用了错误的代码:

Intent returnIntent = new Intent(CMD_SAVE_OBJECT);
setResult(RESULT_OK, returnIntent);
finish();

应该是:

  Intent returnIntent = new Intent();
  // pass the data, remember the key.
  returnIntent.putExtra("extra", CMD_SAVE_OBJECT);
  // Activity finished ok, return the data
  setResult(RESULT_OK, returnIntent);

然后,您需要使用以下方法处理结果:

   @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {

     if (requestCode == 1) {
         if (resultCode == Activity.RESULT_OK) {
            // In case you're sending an int data.
            // remember to use the same key, which is "extra_object"
            int action = data.getExtras().getInt("extra");
            switch (action) {
              case CMD_SAVE_OBJECT:
                break;
            }
         }
      }
   }