When I click back button goto previous activity from current activity android - android

When iam moving current activity to list activity in list activity have 8 items and in list activity have add button widget when I added one item using add button so total 9 items in list activity. Here my problem is when I click on back button my application not moving to previous activity showing list activity with 8 items after click again back button moving previous activity how to solve this issue without using intent

override onBackPressed() method the way you want.

When you use realtime database like Firebase at the moment you will have updated list. I think when you click on button you are opening new activity and don't finish last one. So you come back to last view again. Of course if you send your code, you will get proper answer.

You should override method onBackPressed() in the activity where your back button is not working correctly for you:
#Override
public void onBackPressed()
{
finish();
super.onBackPressed();
}
I think that adding method call finish() should solve your problem.

Related

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

Going back to fragment recreates and adds to existing dataset

I have a Fragment and I create some data inside onCreate(). From this fragment I can go to another one by clicking a Button.
The problem starts when I click the back button. What happens then is that it goes again through onCreate() and re-creates a new dataset and adds it to the old one.
The result is that I end up with two datasets instead of one. How can I skip onCreate when I'm coming from back button or is there another way ?
#Override
public void onBackPressed() {
//Check if you're in the button pressed-fragment
if(){
//You're code here
}
}
This overrides the backButton and you can do what you want - create new fragments or load the from backstack and other stuff. Be careful though, people don't like to not being able to quit the app with back button.

How to clear data from activity using device back button

I had created an android app which has 3 activity (A,B,C) in activity A there is 2 autocomplete textview which fetch data from database and button after selecting data in autocomplete text user press button and it goes to next activity which show listview.
Now the problem is when I press the devices back button from listview is display activity A with selected data in autotext how can I avoid the selected data when I press device back button.
Thanks for helping in advance
When you press the back button, activity A will "resume". So, in your Activity A override "onResume"...in the onResume method, clear your fields.
There is nothing with the listactivity's back button as you are trying to clear you field in the previous activity. As you are not finishing your previous activity while creating the list activity so the previous activity is not changing. So while you are returing from the list you are saying the same data as before. to do so as #Dave suggested you could have done the clearing in the oResume method or onPause method. But there is a problem. If you clear your data in onResume or in onPause you data will be cleared for other pausing or resuming reasons like pressing home, or for other applications etc. So you can do any of two
Option 1:
clear the data of the previous activity when you are starting the list activity
or
Option 2:
instead of startActivity call startActiviyForResult and also override onActiviyResult method. So there you can detect when you are returning from list activity. Then clear the data.
To deal with this problem, I would simply override onBackPressed() method, and in there, I would have cleared activity stack as there is no logical explanation to keep activities in memory, and then start a fresh intent to your original activity.
Here is code.
#Override
public void onBackPressed() {
Intent intent = new Intent(this, Activity_A.class); //I suppose they are in same package
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
I hope it helps.
protected void onResume(){
super.onResume();
setContentView(R.layout.activity_main);
}
this also works well..
you should implement startActivityforResut
When you come back to this activity on result you can clear the data

Advice related to back button on the android device

Hey guys I am new to android development, I am currently making an application.The main activity has a list view and all the items are getting displayed in the list view, as they should.Now if any item is clicked or if a new item is added, a new activity is opened up and gets destroyed if the back button is pressed.Now when the main activity is reached and the back button is again pressed to close the application , it instead of closing the application , moves again to the main activity,only this time, the last entry added is not present, similarly on continuously pressing the back button all the entries first get removed, and when all the entries get removed, the main activity closes and the application stops. Any idea why this is happening, I could really use your help. Thanks.
You need to add android:noHistory="true" to your list detail Activity in the AndroidManifest.xml
For example:
<activity android:name=".MainList"/>
<activity android:name=".ListDetail" android:noHistory="true"/>
Alternatively, you could override the onBackPressed method to finish the activity when you press the button.
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}

When i Click Back Button in android i want to make changes in previous activity?

When I click back button in my app I want to change some data in previous activity.
public void onBackPressed() {
super.onBackPressed();
Log.v("Back Button ","Pressed");
}
I am trying to using this but cant help it out ?
do I need to maintain backstack of activities which back button internally does?pls help
i want to change menubar images (wriiten by me bottom of every activty in my app) item which is present in every activty but when I click menubar (clickable) it is not able to change menubar images as per activity changes.
You want to look at startActivityForResult. The previous parent Activity should spawn the next one with this call. Data can be passed backwards via extras (Bundle) in an Intent when returning to the parent Activity in onActivityResult - or you can simply use the result codes.
You can find more information here: http://developer.android.com/reference/android/app/Activity.html#StartingActivities
Better use with onKeyDown. & onKeyDown Example
Just override this android's default method. It'll provide the changes with whatever you want.

Categories

Resources