In my app I want to allow the user to insert some data which later appears in a list. This aspect is organized as follows:
From the Main Activity, the user can press a button which opens the Editor Activity. Here it's possible to construct the data. Pressing the confirm button, the built data are showed in another activity (New Element Activity), in a list.
Well, if the user goes back through the designated button from New Element Activity to Main Activity I want the list to be cancelled, while now, when adding a new element, the former is still there (that's because the array with the data was initialized before onCreate() method - I don't want to change it). I tried to override the onBackPressed() method, but it didn't work. Suggestions?
Here's my code - the onBackPressed() method.
#Override
public void onBackPressed(){
super.onBackPressed();
data.clear();
Log.d("onBackPressed", "called");
}
Clicking the back button just returns to the previous activity. The overridden method doesn't get called.
Thanks in advance
#Override
public void onBackPressed() {
startActivity(new Intent(CurrentActivity.this,NextActivity.class));
}
Related
I have 3 pages in my application. Page A,B,C. When i click next button in A page the page will navigate to B page. In that page there will be some request and response process and Progress indicator will happen. Then when i click next the page will navigate to C page. There also some request and response process and Progress indicator process will happen. Now my problem is when i click back button from page C The page is navigate to B page. But the request response process and progress indicator process is working. Here i don't want do this process when i click back button. Now i have try like this:-
#Override
protected void onStart() {
super.onStart();
Log.v(this.getClass().toString(),"onStart");
}
Here the request and response process is not working when i click back button. But the progress indicator is loading. This progress indicator is continuously rolling. How to disable all the functions. I just want to go back. Do not do any other work. Please help me to solve this issue. Sorry For the poor English..
As you can see, you add a flag Intent.FLAG_ACTIVITY_CLEAR_TOP. This flag will clear all previous activities.
Intent.FLAG_ACTIVITY_CLEAR_TOP - 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.
And also you finish the current activity, so navigating back to page B call B's onCreate method where you have those requests and process stuff. Avoid adding flag and finish() methods.
You don't want to override the onCreate() method for this usecase, why would you do that? The activity is created, and the process will run. What you want is to override the onBackPressed() method, so that you cannot exit the Activity until the process is complete.
Similarly to these: Android: Proper Way to use onBackPressed() with Toast but here you want to make a boolean that is set to false while the process is not complete, and set to true when it's done. Allow onBackPressed() to call super.onBackPressed() only when the boolean is true.
#Override
public void onBackPressed()
{
if(processesCompleted == true)
{
super.onBackPressed();
}
}
You can't disable the onCreate() method in android but you can however override it like you do with the onStart().
I suggest you take a look at the life cycles of either fragments or activities, depending on which you use.
Fragments:
http://developer.android.com/guide/components/fragments.html
Activities:
http://developer.android.com/reference/android/app/Activity.html
It is hard to understand what the problem really is without more code, but basically the code in onCreate will mostly not be called when you press the backbutton since an activity will be paused until the phone needs more resources and then destroys it. If an activity is destroyed the onCreate method will be called and there is nothing you can do to change that.
If my answer dosen't help, please provide more example code.
If all your cards have different activities, the backbutton should work perfectly. However if your activity is brought to the front, nothing will be reloaded, except the onresume. In the onresume you can perform a new loading structure or something you want to achieve.
When you don't have different activities, use the override at onBackPressed(), that will handle the backbutton.
But place some code for a better answer
You can create a new function in which you run the processes and call
it on onCreate and use a Boolean flag to make sure when you go back to
that activity and flag is checked the function is not called. Then
save the value of flag in savedPreference onPause() method and you are
done and onCreate() method load your saved preference.
Something like this
boolean flag = false;
#Override
public void onResume()
{
super.onResume();
// load savedPreferences
}
#Override
public void onPause()
{
super.onPause();
//save savedPreferences
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if(flag == false)
{
//function call
processes();
}
}
private void processes()
{
flag = true;
// do stuff here
}
Basically my MainActivity has a button which will become invisible after clicking and the SecondActivity will be called after a few seconds. However, when I press the back botton, the button on the MainActivity is still invisible. I want the MainActivity to restart/initialize. I knew it is something to do with onResume or onRestart but how can I implement these methods? Can anyone give me an example? Thanks.
You could call finish() on your MainActivity when you go to your second one. Then Override onBackPressed() in your SecondActivity and start the MainActivity again.
#Override
public void onBackPressed()
{
// create Intent and start MainActivity again
}
I think you are looking for startActivityForResult. You can see a sample of usage in the Android documentation or here on SO.
Basically in your first activity you overwrite the method onActivityResult and in it (if the result is OK) re-show the button. Then, in the second activity, set the result to be returned to be OK and just finish it however you like (either by pressing the back button or by calling finish()).
Alternatively:
You may ovewrite the onResume method in the first activity and just show the button every time this method is called (note that onResume is called even on activity's first start, but since the button is already shown in your case - it won't have any effect).
#Override
public void onResume(){
Button b = (Button) findViewById(R.id.myButton);
b.setVisibility(View.VISIBLE);
}
Inside your Activity, simply write
#Override
public void onResume(){
// put your code here...
yourButtonInstance.setVisibility(View.VISIBLE)
}
and put the logic you need to change the visibility inside it
You could set your button as attribute of the activity and make your button visible in the onPause() or in the onResume() method.
button.setVisibility(View.VISIBLE);
I have an app that allows user to select a txt file from a list and then goes off to the internet to get the contents of that file. All works well except when the user accidentally or deliberately presses the hardware back button to go and see the list again.
Now, when the user clicks a new item from the list (a new file that is), instead of loading a the new file, the app continues off from where it was suspended.I do not want that to happen. I know this is related to the life cycle of the activity.
How do I make sure that it loads the new file rather than continuing from where it left off ?
I suppose you're loading the file in onCreate(). You should do that in onResume() instead.
Do not force Activities to close (e.g. use finish()). First, this does not guarantee the Activity will be closed, and second, this is better left to Android to decide.
Another Method to kill an activity is by calling following method.
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
You can override the onBackPressed method in the activity and call finish() to get the desired outcome.
You just need to finish your activity in onBackPressed() method by calling activity.finish();
To destroy activity on back press, use this code.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Destroys activity.
finish();
}
I have a list in my app. In another activity, user deletes a row from database and it returns to the main activity (I use finish() here). When it returns back, the row that has been removed from database is still on the list. If user presses refresh button, it disappears.
How can I refresh it automatic?
Override your original Activity's (the one that is showed when the user presses back) onResume(), so it looks like:
#Override
protected void onResume()
{
refreshListMethod();
super.onResume();
}
refreshListMethod(); is whatever approach you chose to refresh the List since you say:
user presses refresh button, it disappears.
So turn that approach into a method so you can use it wherever needed.
Recreate the Activity When you click a list item, you can call finish() on the ListActivity then recreate the Activity with an Intent after you delete the row in the new Activity
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();
}
});