Child activity return button vs finish ()

I noticed this: I have parent activity A, which opens child activity B with startActivity(intent). In step B, if I will finish()this activity in some way, the parent activity will be loaded again from the initial state, but if I press the back keyboard button, I will return to activity A, as in the state in which I left It.

Here is an example of how I finish activity B:

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
   switch (item.getItemId()) 
   {
     case android.R.id.icon:
        finish();
        return true;
     default:
        return super.onOptionsItemSelected(item);
   }
}

This is how I open action B from action A:

Intent intent = new Intent(thisActivity, toActivity);
startActivity(intent);

And here is the XML manifest:

<activity
    android:name="com.evapp.activities.A"
    android:label="@string/A" >
</activity>
<activity
    android:name="com.evapp.activities.B"
    android:configChanges="orientation"
    android:label="@string/B"
    android:parentActivityName="com.evapp.activities.A"
    android:screenOrientation="portrait" >

    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.evapp.activities.A" />
</activity>

Can someone explain me reverence between finish()and come back?

+3
source share
2 answers

finish() "" , . Android Activity, , finish() , FragmentManagers popBackStackImmediate() .

/**
 * Called when the activity has detected the user press of the back
 * key.  The default implementation simply finishes the current activity,
 * but you can override this to do whatever you want.
 */
public void onBackPressed() {
    if (!mFragments.popBackStackImmediate()) {
        finish();
    }
}

popBackState(), . Immediate , , . false , - finish().

, , , finish(). , , onResume() , . , , , onResume().

+1

"" " " () , onBackPressed.

B , :

  • A ​​( , ).
  • B .

finish() "" B :

  • B
  • A .
  • B .

A , . ( - ).

.

MainActivity.java

package com.example.activitytest;
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toast.makeText(getApplicationContext(), "MainActivity created", Toast.LENGTH_SHORT).show();
    Button launchChild = (Button) findViewById(R.id.btnLaunchChild);
    launchChild.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              Intent myIntent = new Intent(getBaseContext(), ChildActivity.class);
              //myIntent.putExtra("key", value); //Optional parameters
              startActivity(myIntent);
        }
    });
}

@Override
protected void onResume(){
    super.onResume();
    Toast.makeText(getApplicationContext(), "MainActivity resumed", Toast.LENGTH_SHORT).show();

}

@Override
protected void onPause(){
    super.onPause();
    Toast.makeText(getApplicationContext(), "MainActivity paused", Toast.LENGTH_SHORT).show();

}

@Override
protected void onDestroy(){
    super.onPause();
    Toast.makeText(getApplicationContext(), "MainActivity destroyed", Toast.LENGTH_SHORT).show();

}
}

ChildActivity.java

package com.example.activitytest;

public class ChildActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_child);

    Toast.makeText(getApplicationContext(), "Child created", Toast.LENGTH_SHORT).show();
    Button finish = (Button) findViewById(R.id.btnFinish);
    finish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}

@Override
protected void onResume(){
    super.onResume();
    Toast.makeText(getApplicationContext(), "Child resumed", Toast.LENGTH_SHORT).show();

}

@Override
protected void onPause(){
    super.onPause();
    Toast.makeText(getApplicationContext(), "Child paused", Toast.LENGTH_SHORT).show();

}

@Override
protected void onDestroy(){
    super.onPause();
    Toast.makeText(getApplicationContext(), "Child destroyed", Toast.LENGTH_SHORT).show();

}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/btnLaunchChild"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/launch_child" />

</RelativeLayout>

activity_child.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<Button
    android:id="@+id/btnFinish"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/finish" />

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitytest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.activitytest.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.activitytest.ChildActivity">
    </activity>
</application>

</manifest>
+1

All Articles