Restart Activity with back button - android

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

Related

Overriding onBackPressed() not working

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

How can I preserve previous android intent value when I press the back or up button?

I have a list view in the previous intent and when I tap on that it go to the next activity. Now when I press the back button or up button it comes back to the previous activity. But when I tap the list item again it give me run time Unexpectedly app closed error. Please help me.
It's hard to answer without code, but maybe it's because you don't finish your next activity when you press the back button
It's because of Activity life cycle...
When you go to the next activity then current activity is paused for you. when you press the back or up button then your previous activity resume.
That's why you have to define your code in onResume() method also where you got stuck. because when you come back onResume method is called
You can try override onBackPressed method in your activities :
#Override
public void onBackPressed() {
// put a breakpoint here and check your listview/activities states
//super.onBackPressed();
}

How to disable or override the oncreate method in Android

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
}

problem with going back to last activity via custom back button

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.

Problem in killing activity

I have developed a small android app with 3 activities. In first activity there has a button(Next_Button). If i click the button then the second activity will appear. In second activity there also a button which will forward to third activity after clicking it. In third activity there is a button (Home_Button). If i click this button(Home_Button) then first activity will appear. Now i want to kill second activity when i click the Home_Button in the third activity to make first activity visible. How can i do this?
Please help.
Best wishes
Md. Fazla Rabbi
In third activity, write the below code for your Home button click event:
btnHome.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(this, firstActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
});
For example, for back key pressed, we can override onBackPressed function:
#Override
public void onBackPressed() {
startActivity(new Intent(this, first firstActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return;
}
Update:
For your reference, this one is the same as your requirement: Android Intent.FLAG_ACTIVITY_SINGLE_TOP AND Intent.FLAG_ACTIVITY_CLEAR_TOP,
And one more example:
Go home feature with FLAG_ACTIVITY_CLEAR_TOP
You don't need to "kill" your activity.
In your third activity, if you start the first one with startActivity, you first activity will be shown.
The system will decide if your second activity (or your third) should be paused or destroyed.
just call finish() method of the second activity on Home button click.

Categories

Resources