I have an activity which contains a back button. when it is pressed, it starts another activity which contains a back button in same position. But the problem is it takes few seconds to start activity because it does some heavy task in background. Now the problem is, if the user press back button again before starting the new activity, the second press goes to new activity and the it navigates from new activity to another new activity. I think the problem is clear to you. Any idea how to solve this problem??? Thanx.
Make the Back Button invisible until the new activity is loaded.
You can disable the button when pressed once like
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button1.setEnabled(false);
}
});
It sounds like you are blocking the main thread to perform your "heavy task", I would advise that you start the new Activity, then onCreate fire an AsyncTask to perform your "heavy task".
This way the view will load fast and you can show a dialog to the user to say that it's loading.
Link to AsyncTask tutorial:
http://droidapp.co.uk/2011/05/12/android-dev-pre-loading-with-asynctask/
Related
I have a Main Activity which a FragmentActivity and I have three fragments which are on three separate tabs. On fragment two, I have a button which opens an an activity on which the user can click OK or cancel. Right now when he clicks OK or cancel nothing happens and it just quits the application.
What I would like it to do, is that in onClick() (of either the OK or cancel buttons) of the activity that was opened from the fragment, it goes back to the original activity and back to the same fragment it was on, in this case fragment two.
I right now I have it so that it goes back to the original activity but the first fragment, not the one from which the button was pressed...how can I accomplish this? Here is what I have to go back to the original activity:
btnOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//execute background task for processing
new SaveDetails().execute(); //this actually calls an HTTPRequest
Intent a = new Intent(ACTIVITY_Edit_Trip.this, ACTIVITY_MAIN_CONTROLLER.class);
startActivity(a);
}
});
The tabs are working fine and i can swipe between them, I just don't know how to go back to a specific one from an activity. Is this possible?
Thanks for the help! If I need to post my PagerAdapter code, I can do that, just let me know!
Edit: I have two activities, the main activity and an edit activity. My main activity is a FragmentActivity which is also the one that creates the tabs where the fragments live one. The edit activity is accessed only when the user presses a button from fragment 2.
Once the button on fragment two is pressed, the edit activity opens and it has two buttons OK and cancel, if the user presses either it should go back to the main activity > fragment 2. Right now with the code I have posted, it goes to main activity > fragment 1.
I am creating an app which consists of main, register, option activities. Main and register activities should be started only once. I got it. Problem is they are again appearing when I just going back to my mobile home page without clicking exit. Even if I do not click the exit and goes back to home, they should not appear again when I entered!! Is this possible? I am not able to get the solution for this. Any one please help me.. Thankyou
you can override the method onStop() and put finish() there.
So, when the activity will be not longer visible, it will finish. Or you can put finish() after startActivity() call method.
you can do this by.
Button button15 =(Button)findViewById(R.id.button5);
button15.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
}
}
);
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)
I have a custom back button that for the time being does nothing other than going to the previous activity on the back stack. Heres the code for the button :
backButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
System.out.println("!!! BACK !!!");
finishActivity(0);
}
});
The problem is, its not working. There is simply no change.
Can anyone kindly tell me what I have done wrong here ? Thanks.
finishActivity finishes an activity that you had previously started with startActivityForResult(), it doesn't finish the activity you are currently in - finish() does that.
Use finish() instead of finishActivity().
finishActivity() forces an activity that you started via startActivityForResult() to quit, it doesn't finish the current activity. You can also use onBackPressed(), that just calls finish() internally (see the default implementation), so it has the same effect. Useful if you override the back button behaviour though.
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();
}
});