Back Button default code in android - android

I want to back to activity that calls get intent so when i use this code app crashes,
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
But when i pressed back button which is beside the home button, it goes to the before activity the n app won't crash. So i want to know the back button code.
Please help me
The above code works when there is no get intent.
#Override
public void onBackPressed(){
if(you wanto go back){
super.onBackPressed();
}else{
//if you dont want to go back
// do what you need hear....
}
}

onBackPressed();
where u can do the same action button as back button event

Related

Create go back button function like Angry Birds

I'm creating an app in which Go Back Button function like Angry Birds have, I've created popup window, it has close button and other buttons working.
To go back I use:
#Override
public void onBackPressed() {
// Write your code here
initiatePopupWindow();
super.onBackPressed();
}
I know about finish() code that do not go back to pervious Activity
this code is working when goback Button is pressed. Popup Window opens but instantly it go back to previous Activity.
My Question is : How can stay popup window when go back button is pressed and do not leave to previous activity like Angry Bird games has
When user pressed go back button it open the popup window and user have to select one option
i want to make something like that
looking for suggestions
thanks
Remove this line:
super.onBackPressed();
From:
#Override
public void onBackPressed() {
// Write your code here
initiatePopupWindow();
super.onBackPressed();
}

Android 'complete action using' back button press handling

My android app uses
Intent i = new Intent(Intent.ACTION_VIEW, uri);
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
to begin the twitter OAuth process. This code is triggered by a button press. I would like the button to change to a progress bar when it is pressed, which I have implemented.
When the new activity is started, the user is prompted with a 'complete action using' dialog, and if the user presses the back key while this dialog box is showing, they are returned to my activity. I would like to handle this event and turn my progress bar back to a button - how can I do this?
When the user clicks back, the onResume() method of the fragment is called. Because of this, I can change the button back to the normal style.
You can override the method onBackPressed(). Inside it, you can do what you want, so when you press the back button it will be trigered.
An example for your case would be to put this code on your activity:
#Override
public void onBackPressed()
{
super.onBackPressed(); // this will do the normal behavior of the back pressed.
//in this case, it will close the dialog
button.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
}
I hope it helps =)

Back functionality in android

I am developing an application which starts from LoginPage. When user Login then he moves to Main Screen where grid view for different departments are present.
Every page of application except login page has a Footer which have different Icons like Home, logout, etc.
I want to add conditional back functionality using mobile back button. Some conditions are as follow:
1) LoginPage ---> Main Screen ---> On back user should log out and go to Login Page
2) Main Screen --> any department ---> Any Sub deprtment --> If user press Back button then go to back in same order
3) User is any where in application ---> If press home button from Footer ---> Comes to Main Screen --> No back functioality to go on previous page, It should follow condition 1.
4) If User on Login Page then he will exit from application on pressing Back Button
5) If User on main Screen then user should logout and go to Login Page on preseeing Back Button
I have tried with "noHistory=true" in Manifest and with Intent flags in Activity file.
Can any body suggest me best way to solved out it.
shouldn't be a problem, all you have to do is override the onBack function and add the logout process.
not a problem, the normal behavior of back buttons is exactly that.
DO NOT DO THIS!!! BAD BEHAVIOR.
normal behavior of back button.
that was step one.
this is used for exit from application on back press.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
System.exit(0);
}
return super.onKeyDown(keyCode, event);
}
if u want only back then remove System.exit(0) from above code .
By using this you can manage your all condition which one you want.
Use a stack globally to save screens order. Stack must be available in application level. And get the screen order when you click on back button. Write switch case for screen order and start that activity. that's it.
for example.
crate a class class MyStack{
//here declare a static stack
//create setter,getter method for assinging values to stack
}
when starting new activity assing screen value in stack with setter method
if you are starting a activity from main screen assign 1 into stack, you are starting sub screen assign 2 into stack.
when click on back get that value
switch(value){
case 1: //start mainscreen break;
case 2: //start sub screen break;
}
With what I understand, you cannot override the functionality of home button. By default, it minimizes your app with its current state, by calling onPause(). When you open the app again, onResume() is called and starts the app from where it was paused. As far as your back button functionality is concerned, most of the above answers are fine.
Try,
#Override
public void onBackPressed()
{
finish(); //finishes the current activity and doesnt save in stock
Intent i = new Intent(CurrentActivity.this, Login.class);
i.addflags(Intent.flag_activity_no_history);
startActivity(i);
}
Try this to trap events on the back button
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK) {
Intent Act2Intent = new Intent(thisActivity, Activity2.class);
startActivity(Act2Intent);
finish();
return true;
}
return false;
}
on each activity implement
OnBackPress().
Override it and add the functionality you want like logging out, clearing history stack and start new(previous) activity.
I think simplest approach may be to override back button in your "Main Screen" activity so that when back button is pressed you can do :
1. Executing log out logic:
2. Explicitly call your Login Page
This may give the behavior you are looking for.
On how to override back button, you can refer to this link:
http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html
Hope this helps!

Close app when hitting back button on android

So my login Activity is the first screen you see. When you hit the back button, it exits the app, good. So I open up the app again. After logging in, I am now in my main Activity. How do I make it so when I hit the back button now, it exits the app rather than going back to the login Activity?
When you push the new activity, call finish() on the previous, otherwise it will remain on the stack, therefore appearing when you hit back and pop the current activity.
Hope that helps.
try this one
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}

How do I return to a calling activity if the user presses BACK?

My MAIN activity is spawning a child activity that contains a ListView. While this ListView is being populated (through an AsyncTask), an indeterminate progress bar is shown.
However, assuming that I am an impatient user and I press the BACK button, the progress bar is cancelled but I am left with a blank screen. I have to press BACK one more time to go back to the MAIN activity.
I would like the app to go back directly to the MAIN activity by pressing BACK only once. Can somebody point me in the right direction? I am thinking I should call finish() somewhere but I don't know where to put it. Thanks in advance!
Use setOnCancelListener() on Dialog (which ProgressDialog extends).
you should override "onBackPressed()"
it will call when the activity has detected the user's press of the back key.
#override
public void onBackPressed(){
this.startActivity(this,MainActivity.class);
}
this code will call the MainActivity when emulator/ipphone back button pressed...
you can try like this,place this code in your activities oncreate block.
findViewById(R.id.back).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});

Categories

Resources