I have an application with an activity (activity1) that pauses when pressing a button, opening a new activity (activity2) that has two buttons: a menu button and a button to resume the activity1. Pressing the menu button that opens a new activity (activity3) that has a button to get out of the application but instead of closing, it goes back to activity1, restarting it.
How can I close the aplication or close the activity1 inside the activity 2 or 3?
Sorry my English.
thank you very much
When you call your activity, call finish(); straight after:
Intent intent = new Intent(getApplicationContext(),MyActivity.class);
startActivity(intent);
finish();
This will run your new activity, and close the old one in the process.
Use startactivityforResult to start your second activity.
In second activity, when you press exit menu button, finish the activity with result code 1..
In onActivityResult of first activity, check the result code.. if its exit code(1) then call finish...
Pressing the menu button that opens a new activity (activity3) that has a
button to get out of the application but instead of closing,
it goes back to activity1,restarting it.
Closing a Activity from other activity?? Not Possible.
But you can achieve it(manually) by setting a Global Variable..
Like when you press Exit Button in Activity-3,set a Global variable(int),say "int exit" and call finish() in Activity 3,this will land you in Activity-2..Now in the OnResume Method of the Activity-2 and Activity-1,Go On to Check the value of the Global Variable,if exit== 1,call finish() there.else do nothing.
Edit
Global Variable Example
Related
I have 2 Activities in my Android application - Activity A and Activity B.
When the user presses a button on Activity A, he is navigated to Activity B.
I want Activity A to close after going to the next activity and is completely removed from the activity stack.
I tried adding intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); , but this not work as Activity A was still accessible after pressing the back button.
I added finishAffinity(); , this worked, but while transitioning, the previous activity in the activity stacks gets visible and then it goes to Activity B.
Video
(here the application drawer is shown, and then it goes to Activity B) -
Code -
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this);
Intent i = new Intent(MainActivity.this, MainActivity2.class);
startActivity(i, options.toBundle());
finishAffinity();
Any fix so that the previous activity is not visible and activity is also closed ?
EDIT : I tried finish(), finishAfterTransition() and supportFinishAfterTransition() instead of finishAffinity() but still the previous screen is visible.
So I guess there's an issue with finishAfterTransition()
I solved it by adding android:noHistory="true" attribute to the activity (which has to be removed from the stack) in the manifest file solves the issue.
There is no need to use finish() in the java code after this change.
Method 2 -
Refer #Pratyay ' s answer for a way around.
you have to use finish but like this:
startActivity(Your Activity here)
then Finish()
if you do reverse you can see black screen for a short time do this way
#Override
public void onBackPressed() {
finishAffinity();
}
Use this function in Activity B, this will exit app on pressing back button. And don't use any finish(); or finishAffinity(); in Activity A. This will give a smooth transition
I'm creating an app with multiple screens the user will have to navigate through. Specifically, I'm currently working on a set of activities that must link together as follows:
Main Activity -> a button click leads to "CreateCharacterActivity" -> a button click leads to "CharacterMainActivity"
The BACK button on "CharacterMainActivity" should lead back to MainActivity without showing the CreateCharacterActivity again.
This behavior should be similar in other areas of the app, except it should restore the state the activity the BACK button leads to was in before it was paused.
So to simplify, I want it like this.
Activity A -> Activity B -> Activity C
BACK button causes Activity C to return to Activity A without going through Activity B.
I tried doing this:
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
But this just invokes a new instance of MainActivity. When I then press BACK at that MainActivity instance, it takes me back to CharacterMainActivity.
How can I achieve this? I'm assuming it involves accessing the Activity stack?
When you move from Activity B to Activity C, call finish() at the same time you call startActivity on Activity C. This will remove Activity B from the task stack.
You can start the child activities for result and then finish the child activities with the result also with the same request code.
To start the child activity:
startActivityForResult (Intent intent, int requestCode)
for finish all the child activity
finishActivity (int requestCode)
When a return button is pressed in the app I am working on:
public void clickReturn(View view)
{
// ...
// Call an event handler before finishing the activity
_appData.CustomDetail.hookBeforeFinish(this, new ArrayList<CompSubmission>(_submissions));
// Finish this activity
finish();
}
I am not able to modify clickReturn() - the way the application is structured, all I have access to is the hook hookBeforeFinish().
What I want to do is add a dialog to prompt for some input. I can do this in hookBeforeFinish(), but it only appears for a split second. I assume what is happening is I set up the AlertDialog builder, call builder.show() but finish() is being called directly afterwards - so the dialog only appears momentarily.
Is there anything I can put after builder.show() in the hook function such that it won't continue execution to finish()? If I can pause the application flow until someone presses the OK button on the dialog thatwould work better for me.
Thanks
Please comment finish().
Then read and add a dialog as explained here. In your positive button click (e.g. OK button), add finish();
Edit
Since you cannot override clickReturn() or change the class, you can use your hookBeforeFinish() to create a dialog and comment 'clickReturn();' line.
In your dialog, in the listener for the button which expects to finish the activity add clickReturn();.
This way you will pause the activity closing until user decides to do so.
I'm still quite new to android programming. I currently have a button on the home page that takes the user to a second activity. On the second activity, the user fills in two EditText boxes and clicks a button at the bottom of the page that should send information that the user entered with it. The button that returns to the main activity uses the following code:
Intent returnIntent = new Intent();
String strData = "your data";
EditText foodName = ((EditText) findViewById(R.id.foodLabel));
String foodNameString = foodName.getText().toString();
returnIntent.putExtra("data", foodNameString);
setResult(RESULT_OK, returnIntent);
finish();
Where would I put code in my main activity that should run as soon as this button is clicked? And how can I access the information I sent back? Right now it returns to the main activity but does nothing.
If you expect a result from your second activity, you need to start second activity via startActivityForResult() (not the startActivity()). In this case, once activity you launched is finished, Android will trigger onActivityResult() callback within your main activity. All the data you specified via setResult() will be passed in this callback
My Android app defines an activity with an intent filter of android.intent.action.CREATE_SHORTCUT, which lets me show up in the list of shortcuts that the user can add to their home page, when they select "Add Shortcut" from the menu or long-click the home page.
In this activity I have the following code (actually happens in a click event after they pick which shortcut to add):
Intent shortcutIntent = new Intent(this,MyActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.myicon);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyAppName");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
setResult(RESULT_OK, intent);
finish();
The shortcut works as I would expect, until I reboot the device. I'm actually testing on the emulator, not real device. The shortcut is still there after a reboot, so I know I didn't wipe user data or anything like that, but it acts like it no longer has the FLAG_ACTIVITY_NEW_TASK setting when clicked.
Example steps to recreate (assume my app is an email inbox for clarity):
Create the shortcut
Launch my inbox activity from my main menu acitvity, which uses the NEW_TASK flag.
From the inbox activity, click a message to open the view message activity.
Press HOME key
Click the shortcut -- at this point it brings the entire "task stack" back to the front, with the view message activity on top of the stack, clicking back goes back to the inbox activity, as I would expect
Reboot the device
Repeat steps 2 through 5.. now when I click the shortcut, instead of bringing the view message activity to the front, it brings the task to the front, but then adds a new inbox activity on top of the stack. So pressing BACK once goes back to the view message activity, and back again to the inbox activity.
I also tried setting different properties such as singleTask for my inbox activity in the app manifest, but haven't had any luck. Is this a known issue that flags are not saved with shortcuts?
I think I'll try adding a new stub activity that does nothing but launches the real activity with the NEW_TASK flag and then exits, and have my shortcuts point to that instead. However, seems like a lot of overhead, so hopefully someone has a better answer.