ActivityA是 singleTask 活动,可以通过onNewIntent()
接收图片。 ActivityA使用startActivityForResult()
启动另一项活动:ActivityB。 ActivityB( singleTop )有一个按钮:按下时,它会调用setResult()
。一切都有效,除了一种情况:按下ActivityB按钮后,回家,打开图库并与ActivityA共享图像。会发生的是调用方法onActivityResult()
,但意图数据为null且resultCode为 RESULT_CANCELED 。在文档中,我们读到:"如果活动明确返回,那么resultCode将是 RESULT_CANCELED ,没有返回任何结果,或者在操作过程中崩溃了#34;这不是我们的情况。为什么结果会丢失?
package com.test.test2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
private final static int ACTIVITY2_CODE = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onClick(View v)
{
int id = v.getId();
if ( id == R.id.floatingActionButton )
{
Intent intent = new Intent();
intent.setClass( this, Activity2.class );
startActivityForResult( intent, ACTIVITY2_CODE );
}
}
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data )
{
if ( requestCode == ACTIVITY2_CODE )
{
if ( data != null )
{
String test_str = data.getStringExtra("test_str" );
Log.i( "MainActivity", ".onActivityResult(): test_str = " + test_str );
}
}
else
{
super.onActivityResult( requestCode, resultCode, data );
}
}
@Override
public void onNewIntent ( Intent intent )
{
super.onNewIntent( intent );
setIntent( intent );
// ......
}
@Override
public void onSaveInstanceState ( Bundle outState )
{
super.onSaveInstanceState( outState );
}
}
package com.test.test2;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class Activity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent purchases_intent = new Intent();
purchases_intent.putExtra( "test_str", "ciaoooooo" );
setResult( RESULT_OK, purchases_intent );
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
<activity
android:name=".Activity2"
android:label="@string/title_activity_main2"
android:theme="@style/AppTheme.NoActionBar"
android:launchMode="singleTop"
></activity>
</application>
</manifest>