I have written a simple app which has an 'About' screen (activity) that is shown when the user selects the 'About' item in the Options Menu from the main activity. The 'About' screen has a button named 'Close' on it.
The behaviour I want is that when the user clicks the 'Close' button, she/he should be taken to the previous activity (i.e. the activity from which the About activity was launched). For this I launch the About act开发者_如何学Goivity (TFTAbout) from the main activity (TFT) using startActivityForResult();
then in TFTAbout.java
I invoke setResult()
and finish()
, which should in turn invoke onActivityResult()
in the main activity when I click 'Close'.
What actually happens is that when I click 'Close' the main activity (TFT) is not called.
From the logs it appears that the activity TFTAbout does execute setResult()
and finish()
. However, at this very point, onDestroy is called for the main activity. So, my guess is that since there is no launching activity to return to, onActivityResult()
is not being called.
Am I right?
Please refer to Logcat output below (without timestamps for better readabiliy):
DEBUG/TFT(816): onOptionsItemSelected: 'About' item selected.
INFO/ActivityManager(563): Starting activity: Intent { action=android.intent.action.GET_CONTENT type=text/plain comp={com.wirel.tft/com.wirel.tft.TFTAbout} }
DEBUG/TFTAbout(816): onCreate: START
DEBUG/TFTAbout(816): onCreate: END
INFO/ActivityManager(563): Displayed activity com.wirel.tft/.TFTAbout: 3880 ms
DEBUG/TFT(816): onDestroy: BEGIN
DEBUG/TFT(816): onDestroy: END
DEBUG/dalvikvm(606): GC freed 672 objects / 36656 bytes in 90ms
DEBUG/TFTAbout(816): onClick: 'Close' button pressed... Returning to calling activity.
WARN/ActivityManager(563): Activity pause timeout for HistoryRecord{4370efe0 {com.wirel.tft/com.wirel.tft.TFTAbout}}
WARN/ActivityManager(563): Activity destroy timeout for HistoryRecord{4370efe0 {com.wirel.tft/com.wirel.tft.TFTAbout}}
DEBUG/TFTAbout(816): onClick: Activity is finishing? true
Code of the lauching activity (TFT):
@Override
public boolean onOptionsItemSelected( MenuItem mi )
{
int item_id = mi.getItemId();
switch ( item_id )
{
case R.id.mi_about:
{
Log.d( LOG_TAG, "onOptionsItemSelected: 'About' selected." );
/* Start the activity to display the About page. */
Intent i = new Intent( Intent.ACTION_GET_CONTENT,
TFTMetadata.ABOUT_TEXT_URI );
i.setType( "text/plain" );
startActivityForResult( i, 0 );
break;
}
....
}
@Override
protected void onActivityResult
( int request_code, int result_code, Intent i )
{
Log.d( LOG_TAG, "onActivityResult: START." );
super.onActivityResult( request_code, result_code, i );
/* Use the request code to select between multiple child activities we may have started. Here there is only one thing we launch. */
if ( request_code == Activity.RESULT_FIRST_USER )
{
Log.d( LOG_TAG, "onActivityResult: 'About' activity has returned." );
}
}
Code of the launched activity (TFTAbout):
@Override
public void onCreate( Bundle savedInstanceState )
{
Log.d( LOG_TAG, "onCreate: START" );
super.onCreate( savedInstanceState );
/* Set what to do when the user presses a key but the key press is
not handled by the application. */
setDefaultKeyMode( DEFAULT_KEYS_DISABLE );
/* Connect the appropriate resource to this activity. */
setContentView( R.layout.about );
/* Define the click listener for the Close button. */
Button b = (Button) findViewById( R.id.bt_close );
b.setOnClickListener( this );
}
public void onClick(View v)
{
Log.d( LOG_TAG, "onClick: 'Close' button pressed... Returning to calling activity." );
setResult( RESULT_OK, (new Intent()).setAction("About activity has finished!") );
finish();
ComponentName cn = this.getCallingActivity();
if ( cn != null )
{
Log.d( LOG_TAG, "onClick: calling activity = " + cn.getClassName() );
}
Log.d( LOG_TAG, "onClick: Activity is finishing? " + String.valueOf( this.isFinishing() ) );
return;
}
Could somebody please explain to me why my main activity might be getting destroyed, and whether what I am doing is correct?
I had also the same problem. In my case I solved this by changing a line in AndroidManifest.xml file.
Please see MainActivity does not receive result from StartActivityForResult
I hope this will solve your problem.
精彩评论