retaining arrays - android

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.

Related

Storing activities in an array

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.

Android, manipulating previous activity

I have got 2 activities - "A" contains fragment with a list of conversations, "B" represents a conversation (shows it's messages), in B I can also send a new message.
I would like to be able to update A's list of conversations every time a message is sent in B, so when I click android's back button, A activity's view is updated.
The core of the problem for me, is that I'm not starting A through an Intent when I click the android's back button, so I don't know how to get this effect.
Thank you for your help!
When A is on the backstack, there's no guarantee that an instance of A even exists in memory. The answer to the question of how to manipulate A from B is don't.
Some correct ways of doing it:
If your model (the list of conversations) is Parcelable or Serializable, you can pass it between activities via Intent. You can pass it back from B to A if you start B for result and retrieve it from the Intent returned to A's onActivityResult.
Make the model persistent, like in a database or SharedPreferences file.
Put the model in a bound Service. This would be faster than having each activity load it from persistent storage, but you may still need to make it persistent so you don't lose it when the Service shuts down.

Change another activity without open it

Is it possible to add a textView on Activity B without showing it?
What I mean is, when i press a button (on DiallerActivity), then a textView will be added on HistoryActivity without leaving DiallerActivity.
How can I do this?
is it possible to a add textView on Activity B without show it ?
No, not directly.
An Activity is a special case Android class and shouldn't be treated as a normal Java class. Effectively the purpose of an Activity is to act as a framework for a UI so, if an Activity isn't visible then there basically is no UI. In other words, how can something be a user-interface if the user isn't able to see or interact with it?
As logical Chimp suggests, the changes should be with respect to some form of data history (a database or SharedPreferences) and it is the responsibility of your HistoryActivity to update its visual elements (TextViews) next time it is started.
One Activity shouldn't try to modify the look, feel or behaviour of another Activity except indirectly by changing some form of global data or state or by passing data to it if the first Activity is responsible for starting the second.
hmmm - not sure I fully understand your question. Are you wanting to append the dialled number to the 'HistoryActivity' without showing it?
If so, I suggest you separate your data (list of dialled numbers) from the view (HistoryActivity). Then, you only have to add the dialled number to the data record used to hold them (suggest a list). When the user opens the HistoryActivity, then you can just draw as many text fields as required to display the data.

Life of elements in an array of android project

I have arrays which I populate in one activity of the android project. Then I quit that activity to go to another activity. If I come back to activity 1, then will the array still hold values in them or would the values have been gone?
depends on if the system needed those resources to do something else. If it did then no your array will be gone. If it didn't then yes your array will still hold all of the same values.
See: Lifecycle of an Activity

Controlling Activities

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.

Categories

Resources