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.
Related
Good day, i have a question about android that my teacher couldn't answer and i couldn't really find an answer.
Example Program: Shopping List
I Want to create a program where i can press a button and create a new list with whatever name i choose, for example List A then i press it again, List B and so on, until that point it's quite easy, my doubt is how can i create with that push of a button a new List that opens an activity with that given name with a specific template, for example i press List A and it opens an activity for List A with the same template for List B but without the content the data being shared by lists or me having to create the list manualy on the code.
You can use extras for that.
From the activity where you are in the beginning you create a new Intent like this:
Intent intent = new Intent(NameOfFirstActivity.this, NameOfListActivity.class);
Then you can put an extra to that intent:
intent.putExtra("listname", "ListA");
After that you start the activity:
startActivity(intent);
On the second activity you can get the extra you put in by calling this:
String listname = getIntent().getStringExtra("listname");
Then you can check what the listname is and then display different data for list A and list B.
Note: When you put an extra and when you get it you have to use the same String as the name. In this case the extra name is "listname" but you can use everything else too.
I tried to look up something similar to what I want, but I can't find anything...
I want to pass data that I received from my database (echo json_encode($response); ) from one activity to another. The data is displayed in a listview and by clicking one item (in this case a 'category') it will take the user to second activity that displays a list of books (displayed in a listview).
My goal is to display results from received data (selected 'category' from first activity and selected 'book' from second activity) in a third activity (in here the user will have information about the book).
Any help will be great - a link to a tutorial that can help me solve this problem (well problem for me because I am new to all this), an example code or suggestions on how can I look it up...anything.
Thanks :)
You can use Intent for passing data from one activity to another activity. If you want to pass multiple types of data from one activity to another, Bundle will also be another choice for you. Look for tutorials regarding passing data through intent or bundle from one activity to another activity
Here is an Example to get you started
Intent i = new Intent(getApplicationContext(),toActivityName.class);
i.putExtra("Key","value");
startActivity(i);
I am making a small calculation game.
on the main screen there would be a RadioGroup
o Easy o Medium o difficult
Button Continue
then after the user selects this and presses continue then on next page another radio group
o Addition o Subtraction
Button Start
Now I want to display questions depending on the selection of radio buttons from there 2 activities.
How can I do that, for a single radio group I can use changeListener...But here I have to consider values from 2 radio groups simultaneously.
So how to do that. I tried my best to explain this using example and representation too.
Thanks
Use a bundle with Intent extras to pass data between activities.
Intent i = new Intent(getContext(), SecondActivity.class);
Bundle b = new Bundle();
b.putExtra("key", value);
i.putExtras(b);
startActivity(i);
http://developer.android.com/reference/android/content/Intent.html#putExtras(android.os.Bundle)
and in your SecondActivity
getIntent().getStringExtra("key");
http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String)
For the apps that I've been involved with I've been using a application object to store state variables that need to be visible in more than one activity. The application object gets created before any activity objects, and is the last thing to be destroyed when the application is closed, so it's a good place to store state variables. You're allowed one application object per app, and the class type needs to be declared in the manifest. It sounds like your difficulty variable and your add/subtract are state variables, so I'd suggest you go down this path.
You may also want to use the SharedPreferences if you want to save the value longer the current instance.
See for more details.
http://developer.android.com/guide/topics/data/data-storage.html#pref
I'm a very new to Java. I thought I was doing okay but have now hit a brick wall, so to speak.
The idea is I use the 'home button' '_menu' for the user to choose one of 26 formats. The format they choose has 3 variables. Those 3 variables are then used in the first xml view, along with 2 user inputs to calulate another variable. This variable is then used in a second xml view, along with a third user input here, to calculate the final answer.
I have created the 26 choices and if for example I choose option 5, on the emulator screen I see all the correct values associated with this choice. I don't think those values are getting stored anywhere. I say this because if I come out of that view and return back into it, it's not showing, as in my example, choice 5. It's showing its initial state, as though I was going into it the first time. I assume it's something to do with launching this activity from the start but is there anyway around this. Or really where do I start.
My second question is with the integer variables that I created from this choice. I need to pass them into another java file for the first set of calculations. I've tried to pass the variables/data with the, 'new intent putExtra' but can't get it to work. But I don't think I can use this anyway since the I don't want to launch the second view directly from the res/menu/ .xml view.
Not sure if this is making sense to anyone. Can someone help point me in the right direction?
I don't quite understand your first question, but it sounds like you're launching a new activity and when you quit and come back to it, everything is reset. If you're launching a new activity after selecting the options from your phone's menu button, you should implement a method that saves data to the shared preferences of the main activity. This method should be called on the activities onPause(), onDestroyed(), or onStop() method. You can also add a method on onResume() where the activity checks if there's any data stored in shared preferences and if so, make the desired changes.
As for your second question...I kinda don't understand it either. new intent and putextra is used when you're starting a new activity and want to pass data to it. Views are not "launched" they are only inflated and brought on display whenever you want. I did an app once where I had everything in one activity and just using setContentView() method all the time. In the long run, it just complicated everything. It is easier to keep things simple and launch activities. Here is an example of some variables being passed to a new activity:
On my main activity (FirstActivity) I have:
(when newActivityButton is clicked)
case R.id.newActivityButton:
Intent mIntent = new Intent(FirstActivity.this,SecondActivity.class);
String[] luckyNumbers = {
luckyNumber[0].getText().toString(),
luckyNumber[1].getText().toString(),
luckyNumber[2].getText().toString(),
luckyNumber[3].getText().toString(),
luckyNumber[4].getText().toString(),
luckyNumber[5].getText().toString()};
mIntent.putExtra("luckyNumbers", luckyNumbers);
mIntent.putExtra("message", messageField.getText().toString());
FirstActivity.this.startActivity(mIntent);
break;
luckyNumbers[] is an array of textviews.
Then on my NewActivity onCreate(), I have:
message = getIntent().getExtras().getString("message");
Log.i("TAG", message);
luckyNumbers = getIntent().getExtras().getStringArray("luckyNumbers");
cv.setLuckyNumbers(this.luckyNumbers);
cv.setMessage(this.message);
where cv is just a custom view I created with its own methods
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