I have a main activity, that when I click on a button, starts a new activity, i used the following code to do so:
Intent intent = new Intent(this, SettingsAc
-
{ getApplicationContext.finish(); }
Try this method..
讨论(0)
-
I believe your second activity is probably not linked to your main activity as a child activity. Check your AndroidManifest.xml
file and see if the <activity>
entry for your child activity includes a android:parentActivityName
attribute. It should look something like this:
<?xml ...?>
...
<activity
android:name=".MainActivity"
...>
</activity>
<activity
android:name=".ChildActivity"
android:parentActivityName=".MainActivity"
...>
</activity>
...
讨论(0)
-
This closes the entire application:
this.finish();
讨论(0)
-
You can go back to the previous activity by just calling finish() in the activity you are on. Note any code after the finish() call will be run - you can just do a return after calling finish() to fix this.
If you want to return results to activity one then when starting activity two you need:
startActivityForResults(myIntent, MY_REQUEST_CODE);
Inside your called activity you can then get the Intent from the onCreate() parameter or used
getIntent();
To set return a result to activity one then in activity two do
setResult(Activity.RESULT_OK, MyIntentToReturn);
If you have no intent to return then just say
setResult(Activity.RESULT_OK);
If the the activity has bad results you can use Activity.RESULT_CANCELED (this is used by default). Then in activity one you do
onActivityResult(int requestCode, int resultCode, Intent data) {
// Handle the logic for the requestCode, resultCode and data returned...
}
To finish activity two use the same methods with finish() as described above with your results already set.
讨论(0)
-
I think you are calling finish()
method in MainActivity
before starting SettingsActivity
.
The scenario which you have described will occur in following two ways:
EITHER
You have set android:noHistory = "true"
for MainActivity
inside AndroidManifest.xml
which causes MainActivity
to finish
automatically on pressing the back key.
OR
Before switching to your 'SettingsActivity', you have called finish()
in your MainActivity
, which kills it. When you press back button,since no other activity is preset in stack to pop, it goes back to main screen.
讨论(0)
-
We encountered a very similar situation.
Activity 1 (Opening) -> Activity 2 (Preview) -> Activity 3 (Detail)
Incorrect "on back press" Response
- Device back press on Activity 3 will also close Activity 2.
I have checked all answers posted above and none of them worked. Java syntax for transition between Activity 2 and Activity 3 was reviewed to be correct.
Fresh from coding on calling out a 3rd party app. by an Activity. We decided to investigate the configuration angle - eventually enabling us to identify the root cause of the problem.
Scope: Configuration of Activity 2 (caller).
Root Cause:
android:launchMode="singleInstance"
Solution:
android:launchMode="singleTask"
Apparently on this "on back press" issue singleInstance considers invoked Activities in one instance with the calling Activity, whereas singleTask will allow for invoked Activities having their own identity enough for the intended on back press to function to work as it should to.
讨论(0)
- 热议问题