I am developing a quiz app in which when the user presses the next button , he gets to a different activity that is the next question . Also it will have a previous button which takes to the previous question . So what I am thinking is to store all the activities in an array(if thats possible) . Later when I need to add or remove the question from the app then I will only have to change the array and delete its activity file .
That is a really bad idea. Instead, in the activity where the user chooses the question activity, pass data through the intent and deal with the logic in the receiver activity itself. Like so:
Intent j = new Intent (this,
QuestionsActivity.class/*whatever your activity's name
is */);
j.putExtra("questionNumber",13/*Arbitrary number*/);
startActivity(j);
Then in the activity....
if (getIntent().getIntExtra("questionNumber") == 13)
//Do what you want here
You should never, ever maintain references to multiple Activities. The Android system may decide at any time to destroy an Activity and your references will no longer be valid.
Instead you should separate the model of questions from the way they are displayed in an Activity. You can send data to an activity, for example some identifier for the question or even the text of the question. Your Activity subclass should be general-purpose in that it can display any question. This allows you to create one activity for all questions rather than one activity for each question.
You can keep an array of questions rather than activities and then use that array to display the correct information as needed.
Related
I have 2 major activity in my project Connect+Details and Menu. After I switch between the first activity to the second everything goes well. the problem start in returning back from 'Menu' to 'Connect+Details'.
when I use the back key to switch between the second activity and go back to the first everything goes well.
problem start when I use this code:
Intent myIntent = new Intent(Menu.this, ConnectActivity.class);
startActivity(myIntent);
I do go back to 'Connect+Details' but every detail in that activity is lost.
my question is simple, how can i go back to previews activity and still have the details used in that activity.
Edit:
the same thing goes even when I am in the 4th, 5th activity so finish() didn't help me.
You probably want to either save your data in SharedPreferences or in a database then use the saved data to repopulate your Activity. You can use SharedPreferences to save a key/value pair when you leave your Connect Activity and this will persist even if your app is closed.
If there will be a lot of data stored then you may consider using an SQLite DB. This can be a little trickier to set up and use but may be worth it if you are storing a lot of data...especially if its for different users.
Both of these options will persist your data when you leave the Activity or the program. You could also use a static Array but one of the first two options will probably be better for you.
The Storage Options Docs has some decent examples to get you started, especially if you decide that Shared Preferences will work for you.
Edit
Unless something has changed, you can't store serializable objects in SharedPreferences. But Here is a SO link that discusses saving them to a file and reading from there. Maybe this will help you.
Don't use Intent to move from 'Menu' to 'Connect+Details' instead call finish(); from it
Only use intent in first class to move to second ie;'Connect+Details' to 'Menu'
Don't call finish() in Connect+Details
I'm creating quiz app of sorts for Android. The questions shall be posed in a random order, but every question should only appear once per run. For randomization, I'm creating a randomized array, with the index of each question in a separate class. This array is being passed on through an intent to the main class which is displaying the question.
Once the user has answered a question, I want to give him feedback. I do this by launching an activity telling him if he was right or wrong. After that, the question-activity is being restarted. The only problem now is that the randomized array is now gone.
How do I retain the array for later use? I really could use some help here :)
So you have two Activity classes: question-asking (A), and right/wrong (B).
If you launch startActivity from "A" to get to "B", then don't restart the "A" once you're done with "B". Simply call finish() on "B".
This will ensure that you do not lose your data from the question Activity, and additionally will not start hundreds of Activity objects.
I'm parsing a huge xml to display a list of titles in a listview in activity A. However the same xml also has details for a list item which needs to be shown in a different view (like list mail subjects/view mail details scenario).
On click event of this list i dont want to load a new activity with a bundle, parse the same xml and show detailed view, while i have the required data in activity A itself.
I figured out a way to hide show layouts in my XML to do this as required, but handling back button is an issue. I can probably do this by capturing back button action, but want to know whether there is a better solution for this.
Like broadcasting an intent to A (from A itself) and somehow managing to add that to the activity stack.
Excuse if there is a duplicate question, couldnt find one when i searched.
BTW, i dont want to do a solution with a database caching.
I would handling the back press. Just use a flag within your activity that tells you in which view you are (so back within the detailed view shows you the overview view).
Another way would be to save the values in your applicationContext. Much easier way to do it than database usage.
Take a look at an answer here: How to declare global variables in Android?
But I would definitely go with handling back presses. I have a solution similar to this where I use the same listview in the layout and instead I use different adapters depending on which detailed view the user is in.
Handling back press is the easiest way to go.
Else you could also pass the information to view as Intent extra to the second activity.
Another possibility is to have a local service running in the background and in charge of loading your XML and offering access to its information in a convenient way.
You can also stuff the XML content in an Application object of your own. However I have had not so great experience with that option in some projects.
I would use a second activity. Pass additional data (like contact list, message details, etc.) to it and display it. How you keep parsed XML in memory is up to you to decide (static member? yuck! but it works).
Now back to original Activity. Does your source XML change a lot? Maybe you can parse it and put all data into a DB so that you could retrieve necessary (and hierarchical) data quicker. This way you do not need to deal with storing lots of data in memory, re-parsing and you could perform search faster.
On click event of this list i dont want to load a new activity with a bundle, parse the same xml and show detailed view, while i have the required data in activity A itself.
Cache the parsed XML in a static data member. Your activities that need the data look at the static data member first, then kick off the parsing if and only if that cache is not there.
IOW, this is not an activity problem, but a data model problem. Do a better job with your data model, and your activities can behave naturally.
I have an Activity which is an OpenGL view. I also have an xml layout to use for preferences. Until now, to show the preference menu, I just brought it to front by setContentView(). And the same to get back to the OpenGL view.
But is this a case where I should give the preference menu its own Activity?
I guess this would make a few things much easier. For example, the back button would just work, opposed to now where I have to code it or it will just exits the application.
And if this is a good idea, how do I pass data both ways? I have a class that store all preferences. Can I send it to the Activity and back again? Or is the best way to store the preferences in a sqlite database and then use it for passing data?
I find it easier to segregate menus and such into separate activities (unless you are using dialogs etc..) As far as storing data you can do it a number of ways:
Database
StoredPreferences
Intent extras with putExtra/Bundle
Creating an application subclass and storing preferences there
Each have their merit. 4 is pretty easy as you just have to state the application class name in your manifest then call: MyAppClass app = (MyAppClass)getApplicationContext(); and you can then use any variables in MyAppClass via app. 2 is also straightforward.
You already pointed out the main difference: history management.
You can pass data to Activity via Intents putExtra()/getExtra():
Create an Intend and add custom data via Intent.putExtra(..)
Start the new Activity: startActivityForResult(intent).
Inside new Activity you can get extra data with intent.getXyzExtra() (where xyz is type).
When new Activity is done just call setResult(int, resultIntent). Again you can add extra data as described in 1.
Call finish() to end the activity.
In original Activity method onActivityResult will be called. Again extract data from Intent as described in 3.
I'd like the activity stack for my app to contain multiple instances of the same activity, each working on different data. So I'd have activity A working with data a, b, c and d in my activity stack ad I'd have 4 instances of activity A that I'd call A(a), A(b), A(c) & A(d). I'd also like to arrange it so that if the user asks to work with data c again then it won't start a new activity, but rather will just bring the already running activity A(c) to the front.
Any suggestions on the best way to achieve this?
So I'd have activity A working with
data a, b, c and d in my activity
stack ad I'd have 4 instances of
activity A that I'd call A(a), A(b),
A(c) & A(d).
That will happen by default.
I'd also like to arrange it so that if
the user asks to work with data c
again then it won't start a new
activity, but rather will just bring
the already running activity A(c) to
the front.
I do not believe that is possible unless you create distinct activities for each letter.
I'm not sure you can do it the way you have it described because fiddling with the activity stack like that is not supported AFAIK.
What you could do instead is just use a tab based activity. Each tab could be another instance of activity A working on a different dataset.
I agree with Falmarri (comment), you cant "switch between activities" in the way you are describing. You can however store that data somewhere (file, database, service, global variable, ext.). Where you choose to store that data (a, b, c, d) is up to you and depends on what kinds of functionality you need your data to have.
As for how you "switch" from one to the other, that is somewhat easier than you might think. you dont actually have to "switch" from one activity to the other, you can just swap our all the data. its is perfectly legal (though not always recommended) to have your entire app exist in ONE activity, and merely switch layouts over and over.
My suggestion would be to swap out the data within one activity. you could even specify which data set you want to load initially in your intent filter.