I have a form application, in this application the user clicks on a Menu option(M1) to go to a form (Frm1). This is done using Intent.
The form (Frm1) have two buttons, a back button and a submit button. When the user clicks the submit button the form is submitted, when the back button is clicked the user reaches the menu option(done using Intent).
Now, When the user clicks the back button(he goes to M1) with a half filled form, he must be able to continue with the task when he returns back( to Frm1).
My logic was to collect all the values in the form to a bundle and carry it along the way from Frm1 to M1 and vice versa.
Is this a good approach, got any better idea?
Thanks in advance..!
If data entered in the form (Frm1) is used in your Menu Activity (M1), then obviously you should use bundles and send it between Activities.
Else it may be unwanted logic of working with forms data in Menu Activity.
Just imagine, that you will create new wonderful Activity before M1 in your app (dashboard or something similar to it). Now you have to pass your bundle to first activity, because else you will lose form's state, when user close Menu Activity. It's not good, anyway.
So, if you can encapsulate this logic in Form Activity, I recommend you to make it.
You can use SharedPreferences or Singleton storage. If your form's data is strings, numbers and flags - SharedPreferences is easy, good and safe solution.
Using a bundle is exactly the right way to do this, though if the user has pressed the back button, are you sure they want to keep the data they entered? You will have to provide some way to clear the form.
You can use either Bundle or SharedPreferences.
But better to go for SharedPreferences because Bundle is restricted to the session where as shared preferences is not.
Another option would be to save the form data in the SharedPreferences. The difference between saving it in a bundle and this is that the data will persist upon an app shutdown. Saving the data into a bundle will only last during the application's lifecycle
Related
Being very new to android I have limited experience despite having built several basic apps, so I would be grateful if you could structure any replies with this in mind.
I would like to: after a signup/login section, Collect user selections from spinners, radio buttons and text input over two or three Activities and in a fourth activity display the collected data so that it can be checked going back to make changes if need be.
When correct move on to a fifth activity where the data is structured and emailed to an email address given on the signup section. I have made the basic structure including Spinners with String arrays and have indeed made an app that works as required, passing data with extras to the next activity, however I would like to pass data to the final activity not just the next one in line. I do not really want to use a database if I can avoid it, but possibly so when all the data has been collected and reviewed, and having tried Sharedpreferences when things go wrong and the app crashes I struggle to find out why, so in an attempt to make things clearer I've stripped all that out back to basic structure leaving just working spinners, radiogroups and navigation buttons and here I am.
Being that the app structure I have left has 5 Activities and 5 class files I was hesitant to post the code as I was not sure that was the done thing, however I am willing to be guided on the question.
In your first activity, after collecting the data from the user, you can add them to your Intent using Intent.putExtras(Bundle). This is an example using a username that the user entered in an EditText.
String username = myEditText.getText().toString();
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle extras = new Bundle();
extras.putString("username",username);
intent.putExtras(extras);
startActivity(intent);
In the second activity, you can get the data from the extras in your onCreate like this:
Bundle extras = this.getIntent().getExtras();
String username = extras.getString("username");
When going to the third/fourth... activity, you can do the same steps to carry your data in the Intent you use to open the activity. First get the data from the extras, and then add them to the extras of the next intent.
You would probably want to make the variables global or because you will probably want to use them inside an onClick where they will not be accessible.
I am working on an App for collecting Customer Details like personal, business, assets etc.
The app shows a ListView with options and based on options Activities are started. After entering details in personal user presses back button and returns to MainActivity (Activity with a ListView) and selects another option like business details and assets detail. User enters the details and come back to MainActivity by Back button and selects another option.
I tried to save my Activitys using onSavedInstance(Bundle) and onRestoreInstanceState(Bundle) and sharedpreference but failed.
Can anyone help? I will provide code if required.
OnSaveInstanceState()/OnRestoresInstanceState() is not supposed to be used on a back key pressed from an Activity. In fact , OnSaveInstanceState() is never called. Here the user is explicitly destroying the Activity and removing it from the back stack.
These two methods are used in case where user presses home button or in cases where Android System destroys your Activity.
The options you can try
Try using StartActivityForResult()/OnActivityResult() methods. Here the target activity can set data whcih can be accessed by your parent activity.
Use an application class(a single instance class (singleton class) for holding data).
Things like shared SharedPreferences, files, database, etc.
I'm creating an app where I display a list of pending challenges. When the user clicks on a challenge, he can accept it or ignore it.
Here's what I want to do and I don't know how :
if the user accepts or ignore the challenge, call this.finished and remove the challenge from the list
if the back button is pressed, do nothing, the challenge is still visible
In short, if the user really responds to the challenge I don't want it to be displayed in the list, but if he doesn't choose any option and press the back button, he didn't choses one of the two actions so I want that challenge to still be visible in the list.
I don't think it's possible to detect what button I've pressed when i go back to my main activity. I've thought about using global variables, but I don't want to misuse them either.
Just to be clear, I'm not asking how deleting a list item. But when to know deleting one depending of the actions of another activity.
Give your second activity the index you want to remove as a parameter inside the intent and let it finish by returning the index again as an intent extra (by using setresult(Intent i) and then calling finish) inside your first activity catch the result from your second activity by overwriting onActivityResult (http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent))
see 3.3. Retrieving result data from a sub-activity in http://www.vogella.com/tutorials/AndroidIntent/article.html for a detailed howTo
I'm making an app that has just one Activity. When I press the Home or Recent Apps button, the state is correctly saved and when I go back to the app everything is there. When I press the Back button and return to the app later, savedInstanceState is always null in onCreate. I've read that the state is not saved when the back button is pressed, but this seems unintuitive in my case considering that the back and home buttons are equally valid ways to leave the app in the user's eyes, until they try and go back to what they were doing. Is there no way to save state when the back button is pressed?
If I must do some persistent storage, then:
a) How do I override the back button behaviour to make it save the app when back is pressed, and
b) Can I persistently save a Bundle somehow so that I don't have to duplicate the code for state saving?
What you are searching for is SharedPreferences I think take a look at it. It will help you solving your problem
I may be wrong, but my understanding is that savedInstanceState only saves your data when you change your device orientation, but as soon as you click back button, your activity is destroyed. In order to save your data when back button is pressed, you could save your data by using SharePreferences or you could use singleton class.
http://developer.android.com/reference/android/app/Activity.html take a look at an activity lifecycle first. Sounds to me like you need to override the ondestroy method and persist your stuff.you can use a bundle for simple stuff but complex stuff it's best to use parcel.
The oncreate method should check stuff to see if it's null
I'm making a function in my android app that, at a home screen the user has the choice to view a league or choose a team. I want to know if its possible to and how to store their team selection so that it doesn't go to that activity the next time they start the app.
Also, how would I implement a menu button to change that team if they so wish?
SharedPreferences will allow you to store your league/team selection. You can retrieve this when you start your application and move to your next activity if it is set.
Look how to create Menu items in Android here. These are straight forward and very easy to implement. Just have that menu button launch your "select a team/league" activity
This should be something trivial that you should have been able to google:
For persisting simple data you can use SharedPreferences