I have 2 activities.
MyMain
MySecond I call MySecond activity in MyMain activity. And what i want, that when pressing back button in MySecond activity, it doesn't return to MyMain activity, but return to the screen where MyMain activity was called. I found a way to ovverride it, so it just opens home screen by this:
#Override
public void onBackPressed() {
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
return;
}
You may want to try this:
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
That will move not only the current activity but all activities in your task (which would be your app) to the back of the activity stack. It should then return you to wherever you were before your MyMain activity started.
Related
I am working an android app that has one activity and multiple fragment so I added a lockout feature which locks out the user if there is no interaction in ten minutes. I did this by creating an activity that extends AppCompatActivity and navigates to lockScreenActivity if there is no interaction with the app. Now after the app is unlocked from a web socket I want to navigate back to the activity retaining the current fragment before navigation. How should I go about it.
Searches in stack overflow suggests that I do:
startActivity(new Intent(this, MainActivity.class));
This does not work for me, calling finish on LockScreenActivity did not work too.
Thanks.
If you are going from a Fragment to your MainActivity you should do this
Intent intent = new Intent(getActivity(), mFragmentFavorite.class);
startActivity(intent);
But if you are going from Activity to Fragment , do this
Intent intent = new Intent(this, mFragmentFavorite.class);
startActivity(intent);
if you are doing a finish() going to lockActivity and doing another finish inside lockActivity , there is no activityes in the stack to inflate, maybe thats why is not working for you
If you want to go back from Activity to Fragment.
This is very simple just override onBackPressed() in your activity and call onBackPressed where you want. See my code bellow this will more clear your concept.
#Override
public void onBackPressed() {
super.onBackPressed();
}
//now call onBackPressed to navigate from activity to fragment
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
I want to navigate to another screen when user touch back button. I found a method for this.
#Override
public void onBackPressed() {
// do something on back.
return;
}
I'm navigating from a fragment.But it is not clear that where I have to use this and how. Please help.
You'll need to add that method to the Activity class of the screen you're navigating from.
Then, in that method, add your intent code to move to your second Activity (let's call that Activity2 below) using startActivity
#Override
public void onBackPressed() {
Intent intent = new Intent();
intent.setClass(this, Activity2.class);
startActivity(intent);
}
Just override the method onBackPressed and navigate to the screen you want to via intents or whichever way you prefer.
#Override
public void onBackPressed() {
getFragmentManager().beginTransaction.replace(thisfragment,new Fragment).commit();
}
The above code will replace your current fragment with the new fragment to which you want to navigate.
Lets say I have a a main activity FirstActivity (when I press the back button here, it just closes the app) and I have another activity called SecondActivity (FirstActivity goes to this when user is logged in). Now when I press the back button, it goes to FirstActivity instead of closing the app. Is there any way I can just close the app from SecondActivity, ignoring any Activity on the back stack? The callback method I use to close the app in my SecondActivity:
#Override
public void onBackPressed() {
finish();
}
The best thing to do is when you start the SecondActivity in the FirstActivity, do this:
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);
finish(); // finish FirstActivity
Using this, the FirstActivity will be destroyed when the SecondActivity is created.
You could use something like startActivityForResult kind of like what I had to do in this question, or you could do what I've done here:
public class FirstActivity {
public static boolean close = false;
#Override
public void onResume()
{
super.onResume();
if (close) {
finish();
}
}
}
public class SecondActivity {
#Override
public void onBackPressed() {
FirstActivity.close = true;
finish();
}
}
I'm starting a Fragment activity with 2 fragments from a menu item of an activity.
How can I return back to this activity by pressing the back key? With normal activities this happens by default but with fragment the first activity is destroyed.
You must use an Intent when the method onBackPressed() is calling, for example :
#Override
public void onBackPressed() {
Intent i = new Intent(getActivity(), yourFirstActivity.class);
startActivity(i);
}
getActivity() is optimizing for Fragment.
Hope this help
When you press the back button while in an activity, by default, does the application not go back to the activity that called it? I am calling an activity in my application(call it Activity B), from Activity A, but when I hit the back button while in Activity B, I am taken back to the main page of the application.
So I guess in general, does pushing the back button on your phone take you to the calling activity?
Calling activity B from within an inner class of activity A:
class HeadlineButtonListener implements OnClickListener {
private Story story;
public HeadlineButtonListener(Story story) {
this.story = story;
}
#Override
public void onClick(View v) {
Intent myIntent = new Intent(HeadlineBoard.this, StoryView.class);
myIntent.putExtra(Constants.STORY_EXTRA, story);
HeadlineBoard.this.startActivity(myIntent);
finish();
}
}
You call finish() on first activity after firing the next activity, this will cause it to be removed from the activity stack, just remove the call to finish():
#Override
public void onClick(View v) {
Intent myIntent = new Intent(HeadlineBoard.this, StoryView.class);
myIntent.putExtra(Constants.STORY_EXTRA, story);
startActivity(myIntent);
}