Android 'complete action using' back button press handling - android

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 =)

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();
}

Back Button default code in 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

How to show alert Dialog when home button is pressed ?(ICS)

I want to show alert when user presses home button on device(do you want to exit the app ?)
How to do it ?
You can't handle Home button click, because of Android policy (Home button click event handling android). Of course, you can use onPause()/onStop() method of your current Activity, but your application will be moved to background too quick and user will not see your dialog, I think.
Also, note that Home not closes the app - just moves to background. User usually close app by pressing Back on main activity, try to handle it:
#Override
public void onBackPressed() {
// your dialog here
}
Is is not possible for Android apps to override the functionality of the home button. The best you can do is show the dialog when the user presses back in your topmost Activity.
You can find more information at the following SO answers:
https://stackoverflow.com/a/7240268/3214339
Android Overriding home key
Write the show alert dialog code in onPause() it will work perfectly.

how to implement quit functionality in android application

I want to use quit functionality in my android application. Is there any way to kill all running activities on back button press.Actually i am showing a dialog(contains two buttons) on a button click and by clicking the one of the button on dialog box invokes another activity.But the previous activity by which the dialog is shown, is not finished. Is there any way to finish the activity when i click one of the button of the dialog.
How can i use finish method to end the previous activity.This extends dialog.
#Override
public void onClick(View v) {
Intent i = new Intent();
i.setClassName("co.y.c", "co.y.c.activityToStart");
v.getContext().startActivity(i);
dismiss();
});
You can call Activity.finish().
you can close your app/activity by calling this.finish().
Cleanup should be done within onPause(...) / onFreeze(...) / onDestroy(...) depending on what you want to do.
Regards,
Arun Kumar
If you don't want the previous Activity to be shown on the back button press, you can specify this in the AndroidManifest.xml like:
<activity android:name="sample.activity.MyActivity" android:noHistory="true" />
This will prevent the Activity from being put into the history stack and allow the back button to quit (assuming you add the noHistory flag to every previous Activity)

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