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.
Related
I'm building my first Android app in Android Studio (with Java) and I need some help understanding how to pass data through multiple activities.
The setup of affected classes/activities is this:
MainActivity: Opens a dialog called AddDialog (by creating a new Instance of it).
AddDialog (extends AppCompatDialogFragment): A dialog that has some buttons. One of them launches a class called BarcodeScanActivity (using intent).
BarcodeScanActivity: Simple activity that scan QR codes.
I want to pass the list of QR codes scanned by BarcodeScanActivity (I store them in a String Array) to MainActivity in order to use them, although BarcodeScanActivity is launched from the dialog that gets destroyed once buttons are clicked. Because of that I'm unable to set some startActivityForResult on the dialog and chain the result (onActivityResult) back to the MainActivity.
Also since MainActivity launches the AddDialog by creating an instance, I can't set a startActivityForResult there too.
I have tried adding Intent.FLAG_ACTIVITY_FORWARD_RESULT while launching BarcodeScanActivity from AddDialog, hoping that the result from BarcodeScanActivity will be forwarded back to MainActivity where I have created an onActivityResult method since AddDialog gets destroyed, but I don't know if that is even supposed to work as AddDialog is "launched" by a new instance of it, and not by using an intent.
I have thought about using broadcasts as a last resort, although I read that they are insecure, unreliable and not supposed to be used for passing that kind of data.
Any help is highly appreciated! Thanks
I have four activities, ABCD.
The user will initially go ABCD, then the user may want to tap the back button to check something on C before tapping a button on C to take them forward to D again.
The problem I have is that D is running a count down timer which needs to continue counting down and updating on an activity D view. If I currently tap back to go back to C then tap to go forward to D again, the countdown timer is still running but not connected as a new activity has been created for D.
I'd like the user to be able to navigate back to C without it affecting or destroying D so I can keep my timer hooked up to the view.
Finally, if the user clicks back to B, it would destroy both C and D activities (and the countdown timer).
It's quit easy save the timer value and current time on saveInstanceState call back when users leave the activity. When activity recreates use the timer value plus it with (current time - lastSavedTime) and set the timer.
it should work just fine but you can achieve this in better way by using fragments and ViewPager.
In case you didn't used ViewPager before. its a component that let you switch between different views by swiping screen to left or right. By default view pager loads the current view and one near it in both side. so if you are in page 1 (index 0) page 1 and page 2 is loaded. if your are in page 2, page 2, page 1, and page 3 will be loaded and so on. The good news is you can change the number of pages that will load near each fragment using simple code like this
mViewPager.setOffscreenPageLimit(3);
So now all 4 fragments are loaded. So you have 4 fragments all loaded on screen. According to developers.android.com all the others views (except one on screen) are loaded but in idle state so I don't think you can have your timer inside fragment D. But that wouldn't be a problem because you only have one activity. Implement your timer in the one and only activity you've got and it will work fine because this activity will not be destroyed when pages change. But you still need to save the same data (timer data + current time) because activity may stop due to user switching to another app.
last thing you need to do is accessing timer data inside your fragment. I will not explain how since there is so many tutorial on how to communicate between activities and fragments but just to give you and idea you need to declare an interface inside your fragment, implement this interface in activity and use this interface inside your fragment to access timer data.
The second solution is a lot more complex but it gives the user a better experience and performance increase is noticeable. Decide witch one is best for you. Good luck
You can use this
#Override
public void onBackPressed() {
Intent intent = new Intent(this, C.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
to put your D activity to background and then resume it from C like this
Intent intent = new Intent(this, D.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
However, you should use a service for this sort of background tasks.
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?
I am a newbie in Android and currently I am designing an application where I have faced a big problem with changing views. I have 3 classes, each one for a different screen. I use a button to change pages but it does not seem to work. Every time I move to the next screen, the variables methods etc of the 2nd screen are located in a different class. Can you please show me the simplest way to do this? Thank you.
Place each screen in different Activity. Start respective activities according to button presses. To pass data between activities use Intent.
Activity Reference
Intent Reference
Simple Example for Activities and Intents
i am making an andriod application in which i need to go from one page to another on a button click. i have tried several things but nothing worked out.
Okay, so given these are different .java files, and each has it own Activity (so, different Activities) what you want to do is call an intent as such:
Intent myActivity = new Intent(class1.this,class2.class);
main.this.startActivity(myActivity);
If its in the same Activity, (which I dont recomend) just call setContentView() again