0

I'm confused regarding with some basic android development concepts, my question is not pointing at a particular code, thats why I dont include any.

  1. Let's say that I have an activity inside of which I have a container in which I load a couple fragments (they are multiple instances of the same fragment), now the activity is populated, and inside one fragment I press a button that opens a new activity, it doesn't matter what may happen in that activity, the thing is that when I press a button it should take me back to the previos activity, I know that pressing the back button or using .finish(); will take me back to my already-populated activity, but I want to know, if that is the correct thing to do, or should I finish the activity as soon as i leave to the next one and when I want to go back create a new instance and repopulate it, if so, where should i store the variables?

  2. Let's say that the fragments that I mentioned are "alarms" for an alarm application, and when I create it I call AlarmFragment newAlarm = new AlarmFragment(); and then I add that alarm to an arrayList in my alarms activity (java class) getListOfAlarms().add(getAlarmsAmount(), frag); which remains on the activity that has the fragment container, the question is, are these variables created in the right place? Because I am leaving them inside the activity itself right? What could happen if the activity is destroyed? I've been told that I should create an SQL database for storing those variables. I am not talking about long term saving but variables that I will be using at runtime

Can someone explain me these concepts a little bit? A link to a place where it is explained will be great too.

Rodrigo Miranda
  • 113
  • 3
  • 13
  • Bro, you can add " android:launchMode="singleTop" " in your XML your your previous activity.This way your problem can be sort out.Try this once. – Bhunnu Baba Mar 10 '16 at 04:55
  • 1
    `or should I finish the activity as soon as i leave to the next one` if you want to navigate back, then don't finish it. – EpicPandaForce Mar 10 '16 at 12:00

1 Answers1

0

Your question seems like it has many parts.

In Part 1, this is what I think you are talking about:

enter image description here

1) how you decide to allow the user to get back to the first activity is really up to you. And 2) what you leave in the Back Stack is also up to you and what you want to define for the users' capabilities within your app. For example, if you want them to only be able to use a button that you define inside Activity 2 container, that is fine. However, you are not required to provide a button in Activity 2, and you are certainly allowed to use the Up Action in the App Bar for navigation. If I were you, I would read more about the Tasks and the Back Stack http://developer.android.com/guide/components/tasks-and-back-stack.html

You also mention this idea of having to "finish an Activity" with .finish(). I don't think that is usually necessary, but it is available to you if you want to use it based on what you decide for your app's logic (and what the user should and shouldn't be able to do).

With the Back button, Activity 1 will appear as if just initialized when you get back from Activity 2. Try it out. Also run some Log statements based on the simple diagram I provided and the Lifecycle "callbacks" (put these methods in your Activities and throw a Log statement in each to get a better sense of where you are in the Lifecycle) http://developer.android.com/guide/components/activities.html#Lifecycle

enter image description here

As for Part 2 of your question, I would try/set-up some of the above first, then start to experiment with a single variable to see what happens to it between Activities. There are a lot of "what ifs" to your question. You don't necessarily have to create a DB to store your variables, but that could certainly be an option. Take a look at the Developer Guide for most of the Data Storage options: http://developer.android.com/guide/topics/data/data-storage.html

If you are concerned about losing data when the Activity is destroyed, I might consider creating a database. Read the following for more info on recreating an Activity when you have gone elsewhere and are returning: http://developer.android.com/training/basics/activity-lifecycle/recreating.html In particular Saving Your Activity State: http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState

There's also an SO post on this: Saving Android Activity state using Save Instance State

Community
  • 1
  • 1
robkriegerflow
  • 716
  • 5
  • 13
  • Thanks you for talking the time in elaborating such a good answer. I believe that I got a better concept now, one more thing you may be able to help me with, the button that I mentioned on the second activity actually deletes the fragment from which it was created (and the item from the arrayList) but I cannot find the fragment by tag or id in the second activity, it returns null, should I clean the fragments when I exit the first activity (onPause) and then repopulate it with the correct amount of fragments when I get back (onResume)? – Rodrigo Miranda Mar 11 '16 at 00:03
  • @RodrigoMiranda When you say the button in the second activity deletes the fragment from which it was created, can you be a bit more specific i.e. are you referring to a fragment in your first activity? Is this something you want? If it's a fragment in your first activity are you concerned about it because you want it there when you're finished with the second activity? In terms of your last question, about cleaning the fragments and repopulating: if you handle your stack properly, you probably shouldn't need to manually repopulate. OnCreate() should presumably handle the set-up. – robkriegerflow Mar 12 '16 at 10:59
  • @isoepitetus Yes, the button is a "delete alarm" button it should delete the fragment from which the second activity was launched and remove the item from the arraylist, the problem is that I am not being able to call fragmentManager from the second activity – Rodrigo Miranda Mar 13 '16 at 16:40
  • It sounds like you've done the following already in Activity #1, but if you haven't done this in Activity #2, you may need to get a reference to the FragmentManager with a call to getFragmentManager(). Then you can create a "transaction" which will allow you to add, delete, etc., fragments. Then, "commit the transaction." See the following link for more on this: http://developer.android.com/guide/components/fragments.html#Managing. Especially look at the second block of code in the following section: http://developer.android.com/guide/components/fragments.html#Transactions – robkriegerflow Mar 15 '16 at 01:20