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();
}
Related
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.
Normally when the back button is clicked it goes to the previous activity and if the current activity is the first activity, the application closes.
I have a splash screen (that is logically my first activity) and then the menu activity loads.
I want to close the program when the back button is pressed on my menu activity (as if it is the first activity) and avoid going back to the splash screen again, but I know that I should not exit the program.
I was wondering what is the functionality of back button on the first activity?
does it put the program to pause?
Avoid splash screens !
Instead of using your splash as the main activity, use menu activity as the main one, and in onCreate() chain off the splash activity, which will close and disappear forever.
When new Activity is launched from first Activity, first Activity is executed until onStop() method, then it stops and waits for relaunch, unless you killed it by calling .finish(), in that case launched Activity becomes first Activity and back button will minimize application on back button press. In order to control what application does on back button press you can override this method in your Activities and implement your own custom behaviour:
#Override
public void onBackPressed() {
super.onBackPressed()
}
Very good example of how lifecycle of Fragments/Activities work can be seen in picture below:
Hope this helps. Good luck.
While you are on the SplashScreen, on the method calling your menu activity I assume you are doing something like startActivity (new Intent (this, MenuActivity.class)); in Java or startActivity(Intent(this, MenuActivity::class.java)) for Kotlin just after that call finish() this will remove your SplashScreen from the back stack
add this attribute to your splash activity (in manifest):
android:excludeFromRecents="true"
pressing back in per activity, transfer control to prev activity in stack (back stack) that not excluded from "Recents".
I have 2 android app, by the 1st app we launch 2nd app's specific activity and after some process we again come back to 1st app, but when I long press to home button, there is 2nd app available when i click on it, again it go with the previous input and perform the same process,but after coming on the 1st app I already clear all the 2nd app data.
Please give me any solution.
Thanks
depending on how you are exiting the second app, you could put in this.finish() in the function that quits the second app. For example if you are exiting using the back button you would implement it like this.
#Override
public void onBackPressed() {
this.finish();
super.onBackPressed();
}
In the manifest for your second activity set
android:excludeFromRecents="true"
This will make your second activity not show up in the recent Tasks list (long press Home butoon).
By default value for this attribute is false.
For other attributes check the link: http://developer.android.com/guide/topics/manifest/activity-element.html
I have created an application that has multiple pages and navigation from one to another represents a crucial flow. I don't want the user to be able to press the back button and escape the activity without first warning him and then finally deleting all stack trace such that when the activity is launched again it starts afresh.
As of yet I have been using something similar to the function below :
#Override
public void onBackPressed()
{
this.finish();
Intent int1= new Intent(this, Home.class);
int1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(int1);
super.onBackPressed();
}
But sometimes when after quitting the application when I launch it again it restarts from some random page or the one from where I quit the application (basically not the home screen from where it is expected to start)
I cannot think of a cleaner way to quit the application other than clearing all the previous activity flags as described in the code.
Any help on the above is appreciated!
EDIT :
Anytime during the flow of my activity if the user presses the back button, I want the control to be thrown back to the main page (clearing all the previous activity stack traces). Such that in case someone re-lanches the application it will re start normally from the main page.
You don't need any of this custom code in onBackPressed(). All you need to do is add this to all of your <activity> definitions in the manifest (except the root activity):
android:noHistory="true"
This ensures that none of your activities (expect the root activity) is recorded in the back stack. When the user clicks the BACK key, it will just return to the root activity.
Another benefit of this is that if the user leaves your app (by clicking HOME or by pulling down the notification bar and clicking on a notification, when he returns to your app it will also just return to your root activity.
Anytime during the flow of my activity if the user presses the back
button, I want the control to be thrown back to the main page
(clearing all the previous activity stack traces).
This can be done just by finishing all the activities as they move forward, except the MainActivity.
Such that in case someone re-lanches the application it will re start
normally from the main page.
Is it like if user is in Activity_5 and uses Home Button and relaunches the app again, MainActicity must appear?
IF so, you can call finish() in onPause() of every Activity except MainActivity
EDIT:
Might not be the perfect solution, but this is what I did to achieve exactly the same(logout in my application):
OnBackPressed() in any activity updates a boolean shared preference say backPressed to true and in onResume() of all the Activities, except MainActivity check its value and finish if true.
#Override
protected void onResume() {
super.onResume();
SharedPreferences mSP = getSharedPreferences(
"your_preferences", 0);
if (mSP .getBoolean("backPressed", false)) {
finish();
}
}
Back Button is used to go back to the previous activity. So i would not override the back button to clear activity stack. I suggest you use a Action Bar for this purpose. Navigate to Home Screen of the application using the application icon.
http://developer.android.com/guide/topics/ui/actionbar.html
Also check this link and comments below the answer by warrenfaith
android - onBackPressed() not working for me
#Override
public void onBackPressed()
{
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
you can use that code, it's work for me!
Is there some reason applying a theme to an activity would affect the code function? I was under the impression that styles/themes merely affected appearance...
I have a listfragment (filled from a database) with a button at the bottom, upon the press of that button a new activity is launched to allow you to edit or add to the list/database.
Intent i = new Intent(getActivity(), Activity2.class);
startActivityForResult(i, ACTIVITY_EDIT);
This second activity works as it should and upon exiting back to the listfragment, said list is updated and the new item appears in the list.
Here's where I start having an issue...
Since the second activity is only a TextView, EditText and two buttons, I thought I would use the dialog theme to keep it from taking up the entire tablet screen unnecessarily. From the manifest file:
<activity android:name="Activity2" android:theme="#android:style/Theme.Dialog" />
This does achieve the result I was looking for as far as appearance of the activity goes, but upon exiting activity #2, the list in the listfragment is not regenerated/redisplayed. The additon to the DB is being done, as I can see when I re-start the app the item I added previously finally appears.
The above addition to the manifest is the ONLY change made.
Any thoughts on why this is happening and how to stop it?
Or you could add code in onResume() to refresh it. When it calls the dialog, it calls onPause() and then calls onResume() when the dialog is exited.
Your first activity isn't stopped and restarted when you launch a dialog, but it is when you launch a full screen activity. Try calling notifyDataSetChanged on the list adapter after the dialog activity finishes.