Press back button on main activity opens other activity - java

In my project i move from main activity to activity A and from activity A to activity B. From activity B using home menu on toolbar i jump back to main activity. Now when i press back button application should exit but it opens the activity A again.

You should make use of the launch flags to manage your activities in the back stack. As far as I understood your scenario, I think you need to use FLAG_ACTIVITY_CLEAR_TOP for starting your main/home activity.
Read more about launch flags: https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP.
Also, take a look this for more details on managing activity back stack on Android: https://developer.android.com/guide/components/activities/tasks-and-back-stack#ManagingTasks

Call the finish() method before starting the next activity to have it removed from the activity. Find more details and options here.

For Kotlin write this in your MainActivity :
override fun onBackPressed() {
moveTaskToBack(true)
exitProcess(-1)
}
For Java write this in your MainActivity :
#Override
void onBackPressed() {
moveTaskToBack(true)
exitProcess(-1)
}
Hope to it will work for you as good as for me

hey i write some code for you
Variables:
boolean backactivity = true;
CODE:
public boolean onOptionsItemSelected(MenuItem item){
if(backactivity==true)
{
finishActivity(1);
backactivity=false;
}else
{
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return true;
}

Related

Prevent ActionBar back button to recreate the MainActivity

How can I prevent the ActionBar back button (we gonna say ABBB) of my SecondActivity to recreate the MainActivitywhen clicked ?
I have a ListView in the MainActivity and it can be edited using the SecondActivity. The problem is that when the user presses the ABBB, the ListView is reset to default...
However, when the user clicks my OK button or presses physical back button, there is not any problem because I'm using finish();
#Override
public void onBackPressed() {
finish();
}
If I use this code... :
switch (item.getItemId()) {
case (android.R.id.home):
finish();
}
...there is the same problem because, I guess, this method is called after "Android's code".
How can I totally override Android ABBB's clicked code to set it to just call finish(); ?
The ideal scenario is, when are you in the SecondActivity (I take it that, this means that you are in Edit mode), and you press the device back button or ABBB, you show a subtle alert to the user saying "do they really want to dismiss the editing", or go ahead and apply the edit done as in the Contacts application.
So that being said, if all you require is to finish() the activity on press of ABBB, the code that you shown above should work fine (though you need to put return true; after finish()). It works for me. Try the below one in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed(); // or finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I put onBackPressed(); because your ABBB with now imitate your device back button.
Or, you can set your parent activity's launch mode as -
android:launchMode="singleTop"
that will do all without changing anything.
Reference

how to close main activity after closing another activity in android?

I am developing an android application. So in there I have button called "Aboutus". when I click that one it starts another activity and show corresponding view.
Here is the code for button Aboutus click event.
aboutus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,AboutUs.class);
startActivity(intent);
}
});
}
And then in that aboutus activity I have back button.when we press that back button it will go to the main activity again.
back button key event goes like this..
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(AboutUs.this,MainActivity.class);
intent.putExtra("aboutus", true);
startActivity(intent);
finish();
System.exit(0);
}
});
here i put some intent extras for some other purposes.In this case they do not matter. So and then again in mainactivity i have exit button. which should kill whole app.
and my exit button code goes like this.
exit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
finish();
System.exit(0);
}
});
Exit button works perfectly (it ends the app) except for one scenario. if we click on aboutus and go to that activity and then press back button on that aboutus activity and then again come back to main activity and finally when i want to exit it won't kill whole app, instead it again goes to the about activity.
in conclusion,
MainActivity---> click aboutus button(no problem in here.this will start aboutus activity)
Aboutus----->click back button(this also works fine. go back to main activity)
MainActivity--> Exit button (not working .it goes to aboutus activity again)
So how to fix this problem?
i dont see why you have to start MainActivity.java ...the user can just hit the back button as long as its on the activity stack. Also The system.exit(0) is bad. Just let the app finish gracefully with finish().
if you want to pass something to the activity when aboutUS is done, you can look up onNewIntent() method.
use intent flag ACTIVITY_CLEAR_TOP to clear all activities on top of it:
Intent intent = new Intent(AboutUs.this,MainActivity.class);
intent.putExtra("aboutus", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent);
FLAG_ACTIVITY_CLEAR_TOP From the doc:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.

Avoid splash screen activity when pressing Back button

In my application, I have two activities. First is a splash screen, which simply shows the application name and few other info.
Upon clicking on the splash screen activity, I'm loading the main activity. My app works fine, but I'm facing a small issue. If I press back button from my main activity, control is going to splash screen activity. But I don't want to show the splash screen activity again, I want to avoid splash screen activity when pressing Back button.
Is it possible? If so how?
In your AndroidManifest.xml file, add android:noHistory="true" attribute in your splash screen <activity>.
As I understand, you want the splash activity to not show after changing activity. You should note activities save On Stack and with starting new activity push on it and with finish you pop on top stack. I think that if you the call finish() method your problem fix as in your splash screen activity where you call StartActivity insert finish() after
public void onClick(View v) {
Intent intent = new Intent(Main.this, Splash.class);
startActivity(intent);
finish();
}
Hope to be useful :)
You can just call
finish();
In your Splash screen when you jump to the second screen.
In addition to the above answers, you should note
that:
1: by calling the finish() method, the Splash activity
will close after execution, meaning that it will not be
available in the stack.
#Override
protected void onCreate(Bundle saveInstsnceState){
super.onCreate( saveInstanceState);
\\ do something here
Intent intentSplash = new Intent(SplashActivity.this, NextActivity.class);
StartActivity(intentSplash);
finish ();
}
You will achieve your aim using the method above
but...
2: If you want to prevent your users from force
exiting the app (pressing back button) while the
splash activity in still ongoing which is the best
practice in android, then you need to call the
onBackPressed () method.
Class NoBackSplash{
#Override
protected void onCreate(Bundle saveInstsnceState){
super.onCreate( saveInstanceState);
\\ do something here
Intent intentSplash = new Intent(SplashActivity.this, NextActivity.class);
StartActivity(intentSplash);
finish ();
}
#Override
public void OnBackPressed(){
};
}
With this OnBackPressed() method, your splash activity will not be force to exit no matter how hard the user try.
I understand, you want the splash activity to not show when you click on back button. First of all you should know that all the activities on android are in form of STACK. So what we need we just end the splash activity after it execute. we can do this by calling finish() method in android studio. here is the solution:
Intent intent = new Intent(MainActivity.this, home.class);
startActivity(intent);
finish();
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler hd = new Handler();
hd.postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Splash.this,MainActivity.class);
startActivity(i);
#by calling finish() method,splash activity will close after execution
finish();
}
},3000);
}}

Intent.FLAG_ACTIVITY_CLEAR_TOP in SherlockActivity not serving the purpose

The Scenario :
I have four activities : A, B, C and HomeActivity. A is my launcher activity. Am using actionbarSherlock, so A,B and C have menu option in the bar.
The flow is :
A-> B-> C --**On submit in C**--> HomeActivity
Now when i press Back button on Home Activity, it goes back to activity B as after clicking Submit in C , am using
Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP ); //Shouldn't this clear A,B and C ??
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
startActivity(intent);
finish();
But i would like to remain on HomeActivity only as data is submitted and then it was started.
Also if menu button is pressed on A,B,C, then HomeActivity is started and in that case, i would like to have default behaviour of Back button(i.e go back to activity in which menu was pressed)
Any insights on how to do this as FLAG_ACTIVITY_CLEAR_TOP not serving the purpose!
(P.S. : HomeActivity is not a launcher activity)
I think you finish your Home Acitivity while you go to the Home Activity -> Activity A. When you are using clear top flag then your Home Activity should be alive in your stack. Please make sure that you are not finish your Home activity.
And also put this code onKeyDown() method in your Home Activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.e("onkeyDown>>>>", "Called>>>>>");
finish();
}
return super.onKeyDown(keyCode, event);
}
check more details check this link:
Other Way
EDIT:
try to put above code into Activity A.
remove finish() method from your code like below:
Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP ); //Shouldn't this clear A,B and C ??
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
startActivity(intent);
And Most Important thing Don't Finish your Activity A launcher
Activity for Clear top method it should be in stack of Activity remain
present in your device.
Hope it will solve your problem.

switch between activity with rotation animation

does anybody knows how to launch a new activity with rotation animation?
I'll try to explain what i want to do:
For instance i've looked for android app exemple in skd sample "apidemos" and i've found a class named com.exemple.android.apis.animation.Rotate3dAnimation.java and com.exemple.android.apis.animation.Transition3d.java. These classes allow me to switch between image with rotation effect.
That I would like to know if there is a way to do the same but instead of image, i will be activity (whith new layout).
The window manager doesn't support 3d transformations at this point; since each activity is a window, animations between activities are window animations, so they are limited to what the window manager supports.
This is how we can accomplish this.
Suppose we want to switch from Activity A to B. First we will animate activity A then we will start activity B in overridden function "onAnimationFinished". This will ensure that activity B is started only after animation for activity A has finished off.
// we will only animate activity A here.
// The activity B will be animated from its onResume() - be sure to implement it.
final Intent intent = new Intent(getApplicationContext(), B.class);
// disable default animation for new intent
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
//Animate A
ActivitySwitcher.animationOut(findViewById(R.id.A), getWindowManager(), new ActivitySwitcher.AnimationFinishedListener() {
#Override
public void onAnimationFinished() {
// Start activity B
startActivity(intent);
}
});
Now override "onResume" function for activity B
#Override
protected void onResume() {
// animateIn this activity
ActivitySwitcher.animationIn(findViewById(R.id.help_top), getWindowManager());
super.onResume();
}
You can see here for working example
http://blog.robert-heim.de/karriere/android-startactivity-rotate-3d-animation-activityswitcher/comment-page-1/#comment-12025

Categories