creating multiple instances of the same activity - java

So I've been looking everywhere to try to allow my app to have a new instance of an activity whenever I want "onCreate" but I haven't found much to help me. I understand that you can use launchMode="singleTop" so you can force everything to be a newIntent rather than onCreate. I tried using taskAffinity but it only allows all of the activities of your app to show in recent, whereas I'm trying to allow 1 activity to be opened multiple times. I believe chrome did this at one point. Everything helps, comment if you didn't understand.

Related

Android: Creating a new instance of a new activity

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.
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?
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.
Your question seems like it has many parts.
In Part 1, this is what I think you are talking about:
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
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

Passing data back into a previous activity in Android Studio

We have been learning how to open new Activities, in class, but so far the second activity runs its logic from the OnCreate() class.
I am wanting to return directly to activity one, from activity two, but run a function in its java when I do so.
All I've been able to find on the matter is how to effectively write my own "back" button.
I am needing to pass a string, as well as an int, that was originally sent from activity one to activity two. I need to compare the received int with the sent int to confirm it is coming from the same source.
My biggest problem is that we were taught to use OnCreate(). This will not work on returning to an Activity, as OnCreate only runs when the instance is first created. If I am creating the instance as I move back, my other function will not work.
You can just use an startActivityForResult method.
Here is a topic of that: How to manage `startActivityForResult` on Android?
Hope it helps :)

Android - How to tell what Activities are currently open?

I'm falling into a bit of a dilemma. As I learn more and more about Android and Java, the more confused I am about the state of the building blocks of Android apps. (i.e. Activity, Service, Broadcast Recievers, Content Providers)
Since all of those building blocks are just Java classes, it's tough for me to wrap my head around them because there can be multiple instances of them. As of late the biggest challenge is with tracking which instance of the Activity is actually being created, started, resumed, etc;. I started playing around with Intent flags, and that just threw another wrench (or ten) into the equation.
Being able to tell their state would be indispensable and even better would be to see the current task stack/process id that the activity is associated with. At this point I'm just guessing whether or not FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP are working properly.
Surely there is a solution. Is there any debugging tool that I can use to get an inside view of my app/process to see which components (specifically Activities) are in existence?
Some notes/thoughts I've had:
Should I create a unique static auto-increment int for each class to try and track it myself?
Is this what I want Get current visible activity to user?
Does this application manually keep track or is it really peaking inside of the process? https://play.google.com/store/apps/details?id=com.novoda.demos.activitylaunchmode
There has to be a debugging tool for this... -__-
Not sure if this will solve your understanding of the topic completely, but these guys did a nice app to try and visually see the main launch modes available for Activities > https://play.google.com/store/apps/details?id=com.novoda.demos.activitylaunchmode&hl=en_GB
Hope this helps in some way to explain things! :-)

Android: Creating a class which handles all Bluetooth related operations

I am trying to write an Android application which uses Bluetooth but ran into some problems. I succeeded in getting things working by following the guide on Android Developers portal. But i want to organize my code a bit and move everything related to Bluetooth to a separate class/src file. I run into a problem already when trying to turn on Bluetooth. As per above mentioned guide this is done with:
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
However my class is not an activity and startActivityForResult is not defined. I can pass my main activity's context to my Bluetooth class and call startActivityForResult on that. This works. But result is then returned to my main activity and again I have to write code into it, instead of my Bluetooth dedicated class.
Alternatively I can make my Bluetooth class extend the Activity class. But then startActivityForResult returns a nullPointer exception (I take it because my "Activity" is not initialized).
I am completely new in Android app development so I am hoping for some pointers about what to look at to help me solve my problem. I would really like to have everything related to Bluetooth in one class/src file so that I can reuse it in any future apps.
All ideas in how to achieve that will be greatly appreciated.
Thank you.
Composition, not inheritance.
Pass the Activity to your BluetoothGodClass as a contructor, store it in an instance variable, and use that to call the respective method. BUT.
What do you really need to do? Does the UI need to control bluetooth? Create a method turnOnBluetooth(Activity activity) and use that. Do you really need to receive the specific result of the activity you called? If not, use a Context or a BroadcastReceiver.

android : Can we implement a single dialog showing error, if multiple activities in a flow crashes?

When a single activity crashes, all the activities in flow crashes.
As all activities crash, each generates its own error dialog, so can we have a single dialog for all crashes?
Try catch is already implemented.
You use a singleton static final object to handle all your diablog boxes across all your activities. One such singleton object is the application object, see http://developer.android.com/reference/android/app/Application.html, but you don't have to use that one if you don't want to.
Also, you have to make sure that try/catch is implemented absolutely everywhere, wherever the android operating system makes calls into your app code. I'll find that I've got try/catch in most places in my code, but somehow when I'm trying to develop quickly I accidentally miss a few places and of course they're the ones that end up causing me a problem!

Categories