Don't kill Activity when pressing back button - java

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.

Related

Kotlin have two simultaneous activities

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.

What is diffrence between back button and setDisplayHomeAsUpEnabled in an activity?

I have two activtiy. When a boolean variable in activity 1 is true then the UI in activity 2 should be update (activity 2 is with fragment). When I back to activity 2 with (setDisplayHomeAsUpEnabled) it works correctly but with back button it does not work. What is the difference between this ways and how I can solve this. I try update the activity 2 with this code but it does not work:
#Override
public void onBackPressed() {
super.onBackPressed();
//update();
}
The Home Button is not meant to behave like the Back Button. See this reference. The Home or "Up" Button should take the user up within the view hierarchy (i.e. if you have a number of multiple choice fragments, don't go back through each of them, but back to the activity that started the first fragment)
Each activity should specify what it's parent activity is in order to properly use this functionality. You can do this through the manifest, or by overriding onOptionsItemSelected() as outlined here
In terms of why your activity 2 may be updating when using the "Up" button but not the back button, this could be because both handle the back stack in a different way. Back will take you to the last thing on the stack and resume it, assuming you don't haven't tagged the activity with a launchmode that alters this behavior. Here's more info on the back stack. If you haven't designated your Activity 2 as "singletop" then when you use the "Up" Button, a new instance of that parent activity is created. See here. This may means that the information is updating after using the home button because it's creating a new instance of the activity, while for the back button - you are not creating a new instance but resuming a previous instance... make sure you implement an onResume() function to properly handle this and update the information.

Android Activity starting parallel?

i have four activities.
like (1) (2) (3) (4).
(1) is first activity or main activity.
i have a bottom bar for these activities.
if i click on (2) i want to open second activity.
after this if i click on (3) i want to open third activity.
and if i click on (4) i want to open fourth activity.
after this if i click on (1) i want to display first activity.
without finishing any other activity or again open (1) activity.
a image for easy understanding is attached..
please open this image in new tab to view clearly.
i want to do this without using tabhost.
can it done by using activity group.
suggest any example or tutorial for this.
thanks Rock Brown
This use case is already built into the platform:
In your AndroidManifest.xml there is an activity element for each activity. In the activity element for 1 set the launchMode:
android:launchMode="singleTask"
This causes the platform to only ever launch one instance of Activity 1 in the app's task (which is a stack of activities.)
When you start activity 1, 2, 3, or 4 set the Intent's flags to include FLAG_ACTIVITY_REORDER_TO_FRONT:
intent.setFlags(intent.getFlags() | FLAG_ACTIVITY_REORDER_TO_FRONT);
This causes the platform to bring any existing instances of 1, 2, 3, or 4 to the top of the activity stack, rather than creating a new activity and placing it on top of the stack.
An activity can be closed at any moment by the os if a memory pressure append and your activity is in the background.
I agree with Aashish that you probably want to look at the TabHost tutorial.
But, yes, you can do it without TabHost.
Activity (1) will get launched first, assuming it is the launch activity as defined in your manifest. You can switch to one of the other activities at any time using Context.startActivity(Intent).
You can even do this before (1) is displayed based on state preserved in a bundle or saved to user prefernces (i.e. so it restarts on the same activity that was last used). In a case like this you don't want to the current activity to be on the backstack so you call Activity.finish() after launching the next activity, so now you have.
public void showNextActivity() {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish(); // so ThisActivity isn't in the backstack.
}
You should probably have some code that is shared between your activities to manage the widgets that are used to change between activities (e.g. the Button's) and to handle the switching. Note that, even if each activity has its own resources, you can use the same r esource ID's on each - so the Button that selects activity 2 has ID 'act2' on each activity. This makes sharing code easier.

Why does Android only show the last Activity when using a for-loop?

Perhaps I'm not used to the Android development, but when I look, I expect everything to be done in order. When one activity finishes, the next starts. However, it seems that my code doesn't work that way. Take the following code for example:
for (int i = 0; i < 3; i++){
Intent myIntent = new Intent(Game.this.getBaseContext(), NextScreen.class);
myIntent.putExtra("something", i);
myIntent.putExtra("Opp", oppList.get(i).toString());
startActivityForResult(myIntent, 0);
}
It doesn't display activity one, wait for you to do what you do in that screen, then come back for the second activity. It immediately displays the third activity. If I click on the back button on my android emulator, it will show me activity two... and if I click back again, it will show me activity one... so it just kind of rapid-fires these activities onto the screen without waiting for you to do what you do in these activities. I'm sure I'm not the first person that's wanted to do something like this. Any idea what I'm doing wrong? How do you work around this situation?
Activities execute asynchronously. One way to serialize this is to chain the activites in your onActivityResult method. Pass to each (sub)activity an activity number, starting with 0 and have the sub-activity return it as part of the result. Your onActivityResult logic can deal with the response, then examine the activity number and fire the next activity (if there is one).
I don't believe you can queue up pages like this in 1 handy function. You'd be better off having a function nextPage which takes the current activity have moves the user to the next activity. This could be called everytime the user is ready to proceed to the next page (triggered by a button press or something).
Or just start the next activity on the button press directly

Navigate to previous Activity

I have two screens, in second screen i have one back button that shows the first screen(using intent). if i click that back button from second screen i navigated to first screen no probs in that.. now i want to navigate back to second screen automatically without clicking any button. Please help me how can i do this. Thanks for ur valuable time .
I would give a try to a ViewFlipper activity, with two screens. This, of course, if you can encapsulate your actions into one activity (which i'm sure is manageable with AsyncTasks / Threads, Services and Handlers).
This way you can use gestures, (soft/hard)buttons, dropdowns, -whatever-, to navigate between your screens.
You can automatically go back to a previous activity on the Activity stack by calling the finish() method.
If you want to do this without any user interaction, use a Handler that automatically calls finish() on your activity after a predefined timeout (see this article for more information on Handlers).
try with Thread.sleep([ms]) and then call finish or call intent to the first
The one solution is, when you press BACK button from second screen instead of call finish(); you should start first activity again, so the activity stack will be
FIRST
SECOND
FIRST
so after the delay of two second your activity will finish and eventually will come back to second screen, and state will be maintained also.

Categories

Resources