My app has 2 activities which have no way to reference eachother; what I would like is when Activity B is created, an event is triggered in Activity A (I'm trying to "finish()" Activity A)
Is there some kind of event handler that I can create in Activity A to listen for such a thing?
Only one activity at a time can be active which makes it impossible for one activity to send a message directly to another in real time.
From your description, I think you need to use one Activity that hosts multiple fragments. The fragments can "chat" to each other as much as you like and both can be active at the same time.
Related
I am developing an Android Kotlin application which uses two different activities. In each one I have a button that allow me to move to the other one, to say, in Activity 1 I have a button that calls the follwing:
val intentActivity2 = Intent(this, Activity2::class.java)
startActivity(intentActivity2)
This launches correctly Activity2, which similarly inside it I have another button that calls the first activity to return to it:
val intentActivity1 = Intent(this, Activity1::class.java)
startActivity(intentActivity1)
The problem I have is that I want to have both activities running simultaneously (not needed to be shown at screen at the same time), and the issue right now is that every time I call the "startActivity(intent)" a new activity is created, loosing what I had in the previous one, so when I return to Activity1 everything is reset and the same when I go once again to Activity2. Both activities work fine and do their work, the problem is that I can't freely conmute between them.
Is there a way to have both activities at the same time or to not start a new activity everytime I want to change to the other one?
Thank you,
As someone just said you need ViewModel to retrieve your data after deleting/creating new activities. Maybe you can use fragments to do your things. Fragments are attached to activities and it is easier to use them instead of ViewModel.
I have an activity lets call it activity A which on creation downloads some data from a database. The data is stored in a custom ArrayList called myData.
When a user clicks on a button another activity called B is launched. Activity A passes some data to Activity B which I can do by making my class implement Parcelable.
My understanding when Activity B is lanuched that Activity A will be in either the onPause or onStop state. My question is will my arraylist myData still exist when Activity B is closed and the focus goes back to Activity A? If not how can I store this data?
As soon as you launch a new activity, the previous activity goes in onStop state.
The definition of onStop:
Called when the activity is no longer visible to the user, because
another activity has been resumed and is covering this one. This may
happen either because a new activity is being started, an existing one
is being brought in front of this one, or this one is being destroyed.
See this chart
Now when you go back to ActivityA, the array data will still be present. So when switching of activities takes place this data is not destroyed and recreated once the activityA is again started.
Yes, you can finish you ActivityB by using finish() method. And your data of arrayList will exist when you will be back to ActivityA. And i will suggest to declare arrayList as instance variable of ActivityA and fetch data on onCreate() method. so data will exist until activity destroy, so data will not fetch again when activity come to focus .
Thank you, for reading this ;)
I have a class "scanner" with events that trigger when a barcode gets scanned and three activities. I want to let the user switch from activity to activity, but when an event is triggered in "scanner" i want the "scanner" class to start/resume a new activity (one of the three).
So, basically i want to create a "manager class" which events will always trigger, even if another activity is on the foreground.
How do i do this? I am coming from Xamarin Android so my Java knowledge isn't that great...
Java equivalent for event is an Observer design Pattern. If you look at the button Click handler you'll see listener interface. Listener is an Observer of the event.
If you want to handle navigation from any activity you need to have single navigation manager shared by activities. You can create singleton in Application inherited class. See how this is achieved. And subscribe to scanner events using observer pattern. Then since you need to get access to navigation manager create base activity class and implement there Navigation Manager member and navigation methods. Inherit from this base activity your's 3 activities.
I have an Splash Screen activity that launches an Asynctask that goes off and downloads and processess alot of data. It takes a while (20ish seconds) and it's a refresh of data so I dont wait for it and so send my users to a main activity where they can view the last cached data (with an indicator data is still downloading).
What is the best way (or any way) for the async task to notify the main activity (or any other current activity the user is viewing) that its done and to refresh screen and stop the indicator?
FYI, I dont want to just launch the AsyncTask from the Main as there are 3 other activitys that the user may have navigated to that all would need to know when it is done and to stop showing the indicator.
Your AsyncTask can broadcast an Intent when the data is available. The current Activity can set up a listener for that broadcast Intent and refresh the screen when it get it.
I have developed one application in which i need to register a user in SIP account as well as Chat account. To register the user i need pass through three classes: setting.java (sharedpreferences), sipchat.java (registeration to SIP account), xmppclient.java (to register in chat).
Now i am calling settings.class to register user in application in settings.java I am calling siochat.java and in sipchat.java i am calling xmppclient.java.
This is the way the user registers in application:
code:
Intent i = new Intent(Welcome_screen.this, Settings.class);
startActivity(i);
finish();
When using this application, it takes lots of time to register and it blinks as it passes through different activities.
So how do i call all three of these classes in a single activity? (Because it very weird that at the main screen the application blinks thrice.)
Thanks
I give you some solutions:
Use startActivityForResult(), pass through 3 activities and handle the result in your main activty.Example: http://rahulonblog.blogspot.com/2010/05/android-startactivityforresult-example.html
Change the content view of one activity. In this case, we have 3 views. Example: How to use view flipper with three layouts?
I don't understand why you need to start all these activities. Isn't it possible to just call static functions in the target activities?
Otherwise could you further explain why it's crucial that you start all these different activities and not just handle the functions in one activity?