i just want to start my application from initial state - android

I have 2 activity when I press the image on 1 activity it launches the 2 activity. Each time when 2 activity launch it triggers the http request and form the table.
It works fine but when i press "back" button and then again start the application and came to the 2 activity.
It doesnt clear the previous data of table and add the data which came from http request. I didnt use a database its just a xml tablelayout.
Simply I just want to start my application from initial state.
I dont want the android system to remember the activity's previous state when I backpressed the whole application.
Please help me on this.

then may be i think you should completely close the application on back press so your problem will be solved.
this is the code :--
#Override
public void onBackPressed()
{
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
this method kills the application and Ofcourse your problem!

In manifest, for second activity write android:noHistory="true", then it won't remember history.

if i'm getting correct (from your question) , You need to check the views before making http call e.g let us say u have a textview there in table layout, then check like this before making the http call:
if(getthevalueoftextview!=null){
textview.setText("");
}

Related

Master/Detail Flow Data gone after click on Back Button

I am calling a Master/Detail activity in android by clicking on a button placed in another activity (UserActivity). The strange thing is, that if i click the back button in the Master/Detail activity, I loose the Data in the state of the UserActivity. It wants to execute the onCreate Method again.
If I click on the login-button in the LoginActivity where i am redirected to the UserActivitiy and i go back with the Back-Button of the "Smartphone", the username and password i typed are still there. So there i do not loose the data.
Is there a difference between the back-button of the Smartphone and the back-button at the top of the program? I am a bit confused now and i know how to persist the state of the Activity. But my question is, why i am having this behavior on the one side and on the other not.
Just in case you will leave the question like this and don't add code:
What will probably help is checking for the amount of items that are already there. When I ran into this issue, the onStart() got called so quickly that it seemed to me that the Activity has lost the data. Actually it DID have the data, but calling onCreate/onStart (I'm in a Fragment) NULLed it.
What I did to avoid this is to check if there is a need to load items in the list. If there is, it calls a method that contains what the old onCreate/onStart did. If there is no need to load data, it will just skip the step and live with the "old" data happily for the rest of its life.
#Override
public void onStart() {
super.onStart();
if(DummyContent.ITEMS.size()<3){
initializeApp();
}
}
void initializeApp(){
videoTitles=new ArrayList<String>();
videoUrls=new ArrayList<String>();
. . .
}

using onresume to redirect activity after pressing back

I have an activity that I use when my android app is opening to validate information about the user logging in. There is not a GUI for this Activity and it's intended to redirect to other Activities based on validation information. Somehow my app will go back to this screen and just be stuck on an empty Activity.
I have not coded an onResume.
Would I use onResume() as well as removing it from the back stack to prevent this?
Thank you!
when you successfully Validate user information in first "Activity" before going to 2nd activity ,call
finish(); //this will remove previous Activity from the stack
hope this will resolve your issue
It's absolutely not a good idea to define an activity for validating things.Just to answer your question no its not necessary to code onResume().onResume() is just a method which is called after onCreate() in an activity life cycle.
You can finish() the activity right in your onCreate() to prevent it from loading on press of back button.

Start Android App on same activity every time

I'm working on an Android app that will show college fitness professors how their students are doing in their classes. Since this data is fairly sensitive (biometrics are shown, including weight, something many college students are self-conscious about) I don't want the data to be available to anyone who picks up the tablet. While I have a proper login screen created, complete with authentication for the database, etc. I have an issue when the home button is pressed. Since Android doesn't close a program immediately on leaving the app, it's possible to reopen it and return to where you were. I would like to force the app to return to the login screen each time (I've altered onBackPressed so you can't just return to the previous view from the login screen) so that you have to re-enter your credentials to get back into the app. However, I can't seem to do this. An answer I found on here said to use the following line:
android:clearTaskOnLaunch="true"
However, no matter what XML file I put it in, be it the Manifest or the individual Activity XMLs, it appears to do nothing. So, how do I ensure the login screen comes up each time the app is launched, regardless of whether it is starting from scratch or not?
Try to play around with onUserLeaveHint() method. If you read its documentation, it says:
Its Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called
So, when ever you detected home button pressed, you can finish the running activity/activities. So next time user click the app, it will start from the first login screen.
Hope this helps.
You should override onUserLeaveHint()
#Override
protected void onUserLeaveHint() {
// do your logic
//finish() or start login activity
}
You could set a flag when onPause() is initiated within the activity. And then when you return you could check the flag from within onResume() and then request a login from that point. This will be sure to request it each time; in a simple case of course.
Edit:
With multiple activities, you could check against a saved context to see if they are the same when you start a new activity. If the contexts differ then you can discard the context previous activities context and start a new activity.
If they are the same, then you have come back to the activity from itself (you have lowered and brought the screen back). You would have to use some form of saved state such as that to do it in this manner with multiple activities when outside the case of a simple application.
I found out how to do it in my case. For any others with the same problem, try following the example here:
Android detecting if an application entered the background

Exit application programmatically

I have been searching for an answer but I couldn't find a proper one. The question is that I have a dialog themed activity on top of a normal activity. I would like to force the users to either read and click "OK" to the themed activity which will then transfer them to another dialog themed activity for some further questions or cannot enter the application. So, I would like to exit the application, on back press, and not just finish the themed activity that will reveal the content of my app. How is that possible?
If you start your dialog activity with startActivityForResult() you can send back the result RESULT_CANCELED from the dialog, and upon receiving this (in your main activity) you call finish().
finish() will do the perfect job for you ;)
but make Dialog not cancelable..
Edit (after problem description clarification):
As others said StartActivityForResult could work, with additional trick.
Because of the way you design your App (DialogActivity1->DialogActivity2), it might help to add following line in AndroidManifest file, for all your special dialog-look activities:
android:noHistory="true" or to set flag for intent Intent.FLAG_ACTIVITY_NO_HISTORY before u start DialogActivityN.
Both lines (from manifest or code) will make this actitivies not to stay on android stack, so when your MainActivity get result back, it will be result from last DialogActivity and than depending on result recieved you can either finish() or continue with execution of MainActivity..
when u start activities like this there is no need to call finish() to destroy them, u just start new activity and they will be gone from stack. Of course, in your case, last DialogActivity u will start with StartActivityForResult() and as I explained in previous paragraph MainActivity will do something based on received results.
However making user goes through these dialogs several times at the beginning application, is not something I would consider good practice and it can make your user just give up and go for some less annoying app. (don't get this wrong, it's just my advice to rethink about concept)
Hope you will solve it ;) Cheers
If you know about ActivityforResult then way is easier for you, First you need to start the dialog activity with method startActivityforResult... and then when dialog activity get close by back button you have to close it by Set result. In OnactivityResult method of start activity have to detect the same and close the same if setResult is not as according. Hope you got the point.

Quit application button android

I would like to create a button in my game for the user. The button will quit the application.
Please can someone tell me if there is a way to do this.
Thanks.
Edit:
I have an activity which uses another activity with a class that extends Android.app.Application using set and get methods.
Simply using the back button switches the activities until it goes to the beginning.
I go in between these classes 20 times.
Thats why I needed a back button. But I guess there isn't so I will have to do it the long way and set everything back to the first state on quit. Thanks
There is not a way to make quit button. And there is good reason for that because the Android experience is having the back button do the closing. So you just to make the back button exit back to the home page. To do that you need make sure that your current activity is the only one oh the history stack. Then you can create a button that just calls finish(). Hope the detail explanation helps.
You probably want to mange the activity stack better.
If you look at Activity and Task Design Guidelines
it might help.
Setting the flags when you start each activity is probably the key, code such as
Intent i = new Intent(this, Whatever.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
will limit the number of instances of 'whatever' to one only. ( A different flag might be more appropriate for you depending on how you want your app to run, read up about them all)
Try this:
public void quit(View view) {
if(Build.VERSION.SDK_INT>=16 && Build.VERSION.SDK_INT<21){
finishAffinity();
} else if(Build.VERSION.SDK_INT>=21){
finishAndRemoveTask();
}
}
If I read your full question, you are looking for a Reset button not exactly a quit button. I had a similar issue... the next and previous takes only one step back at a time. I wanted to go back to the very beginning. The way I acheived this is to have a class to manage the pseudocursor.. basically an int that represented which resource to pick (I used a singleton). In the main activity's Menu (android.view.Menu), I added a reset/go to beginning option. This will simply reset the pseudocursor. In my activity class's onResume(), I had the code to get the resource from the singleton. So no extra coding was required there.
Instead of having this option under Menu, you can always have a button in UI which does the same thing.

Categories

Resources