I have two screens, Screen A and Screen B. Screen A can take user to Screen B and Screen B can take user to Screen A. The problem is that I cannot use the previous copy of Screen A when I go to Screen A from Screen B. I want to finish the app when it's on Screen A and user presses back button.
I tried using
popUpTo(ScreenA){ inclusive = true }
but it still takes me to Screen A from Screen A when I press back.
It sounds like there are multiple Screen As. Try this: while navigating, use launchSingleTop = true as a navigation argument right where you have inclusive = true. Do that wherever you are navigating. More info: https://developer.android.com/reference/kotlin/androidx/navigation/NavOptionsBuilder#launchSingleTop()
Related
I'm trying to use Digits in my app and tested this out on a sample app:
SCREEN 1 - MAINACTIVITY:
I press the button to use my number and it goes to the following screen:
SCREEN 2 - DIGITS SCREEN
Now in my test scenario, the user does not enter in a cellphone number at all. He decides not to use my app and presses the back button at the bottom of the screen to exit from the digits screen.
Instead of going back to SCREEN 1 which is my MainActivity, I would like the app to quit.
I'm looking for a callback method from digits to help me do so but can't seem to find one. How do I achieve the effect that I want?
You should clear navigation back stack, when you go to this screen and then override the method onBackPressed() and call there finish().
Check this how to clear back stack.
I would like to save state of an activity when you navigate away from it. For example, you have Activity A with filled in text on it and you navigate to Activity B using an intent. Well when you go back to Activity A using an Intent you lose all the text you had on that screen.
When I use the soft key back button on most android phones, the screens text stays on there, I would like to mimic that method of keeping the text on screen between navigation.
The last thing I want to do is pass parameters between all my screens, there are a lot of forms and the users dont have to follow a specific nav order.
I have 3 screens(xmls):
screen 1 is main menu,
screen 2 is main game and
screen 3 is showing either game over screen or game finished, screen 3's background is blurred transparent on top of screen 2.
All these navigations are creating new activity(view) and using intent to go next screen.
problem1 : if i keep on creating new activities whenever user go to screen 1 to screen 2 and finishes the game and screen 3 will have option to new game which will create new activity of screen 2, in this case, stack will be more and to go out of the game user should press back button in more number (stack numbers). I would like to know though game creates multiple activities, in stack only 3 screens should retain (or when user clicks on back button 3 times, game should come out)
problem2: user clicks on backbutton when blurred screen 3 on top of screen2 should go to main screen (screen1), as per current stage, when i click on back button its going to screen2 (from blurred screen3, which is no meaning for me)
Thanks
Problem 1 : write android:launchMode="singleTop" for each activity tag in manifest . this will avoid creation of multiple instances into history stack , will keep latest entry only .
Problem 2 write android:noHistory="true" in Screen2 activity tag of manifest,so that there will be no entry for Screen2 in history stack and back press on screen3 will show screen 1 . also have a look of Intent.FLAG_ACTIVITY_NO_HISTORY
What you want to do is set the launch mode for these activites to single top in your manifest. In your manifest set android:launchMode=singleTop for each activity you only want one instance of. For more information, checkout this.
I am working on the application where I am using the TabActivity.
Now what the problem I am facing is Suppose I have two Screens say Screen A and Screen B.
Now when I am in Screen A and I enter the Data in the EditBox of it and then I switch to the next Screen i.e. Screen B and then I enter some data in the EditBox of the Screen B also.
Now when I get back to the Screen A, I can view the Data that I had entered in the EditBox of Screen A(Which I can see), but when I again navigate to the Screen B, I need to see the Data that I had entered in the Screen B before coming back to Screen A.
I am not able to sustain the Data in the Screen B once I come back to Screen A and then again coming back to Screen B.
I know the question could be good enough to chew your mind, but the Issue is doing exactly the same for me.
Use Shared Preferences to store your data. When you navigate from one activity to another activity. It may result in DATA loss
So use Shared Preferences
In my application, 4 screens are there. When I switch from one screen to another screen 2 or 3 times. At that time, I want to go to the 1st screen but previous screens are coming. How to close previous screens?
Are you mentioning activity as "screen"?
If yes, you can call finish() to close the current activity