I have a small Android application with a TabScreen as my main screen. I have a small problem where my application currently loses focus on the last Activity the user was on.
This is the scenario:
User launches application
Application shows login screen
User enters login details and goes to tab screen
User leaves application via home key
User presses application icon to return to app and the login screen displays again
I want the application to return to the last known displayed Activity in this case.
What I do at the minute is launch the login screen as the Main/Launcher Actvitiy, then when correct credentials are entered launch the tab screen activity and finish the Login activity.
Is there a launch mode or something I should be using to achieve this?
EDIT: More info
The Tab screen is launched simply like this:
Intent intentTabActivity = new Intent(getApplicationContext(), TabScreenActivity.class);
startActivity(intentTabActivity);
Leaving the application through the home button.
I intend to persist the login state and bypass the login but on smaller applications I have created the application returns to the last displayed activity automatically and does not return to the initial Launcher screen every time and I was wondering why this is not the same behavior in this application.
Also as per my other question HERE the behavior seems to be different for debug and signed releases.
This has always been tested on real devices.
This is the correct behavior. Essentially what happens is as soon as the activity goes in the background it is on the mercy of Android DVM. If DVM feels it needs space it will essentially go ahead and kill your application. So once you try to start the application from the icon it actually restarts it from scratch.
However to solve your problem, you should have a checkbox like "Automatically login" or "Remember password" on the login screen and when the user checks it everytime the app opens it should automatically log you in and take to the next screen. This behavior needs to be implemented by you using some sort of persistent storage.
Might be because you are using your onPause() so that your tabs does some action when the tab is passed.
So eventually when the home key is pressed onPause() will be called which might lead you to this problem. Maybe you will have to check your onPause() for this case.
on home button only onStop is called, are you doing finish of activity on onStop? If not it preserves what was the activity which is top of the stack.
Related
I build an app. There are so many activities. When clicked on the back button of the phone it supposed to exit from the app when in the `MainActivity` but when clicked on back button it switches between the activities visited recently. I added Intents to switch to the activity I need to switch for every activity except `MainActivity`. Now they are working perfectly but I need my app to exit when clicked on the back button of the phone when in the `MainActivity` and also one thing, I have an **Splash Activity** in my app. I made some apps earlier also and there were one `WebView` app and there are only one Activity. In that app also there were a **Splash Activity** and when clicked on the back button it goes to the splash activity and not exiting the app. Also I need to exit the app when clicked on the button.
Please help me!
God Bless!
This can be achieved using finishAffinity()
I'm developing a scientific app in Android Studio. It works smoothy.
The set of source code files is not small, but, as I don't have practically user interface, there is only one activity and there is no intent.
All initialization code is inside OnCreate. Most of times, my app preserves all data, when he gets out of the foreground.
However, maybe (I cannot find a pattern of this event) he loses all data and restart (shows a white screen for 2 / 3 seconds), even if the cell phone don't enter in lock screen and there are just 2 apps running.
There are situations that I comute for another app (like WhatsApp) and resumes for my app, and my data was gone. The app restart again.
There is no error message, no logcat. Nothing.
Mostly, when I lock the screen and enter again, all my app data is there.
PS: My orientation is locked.
PS 2: I've read all related question and there is no hint for me. Based in one answer, I've tried to put in onCreate the following code.
if (!isTaskRoot() {
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
No changes for me.
Update:
I've stumbled in solution. it can be read in my own answer. it's related to undesired back button effect for one-activity-app (read here and here ).
For me, as my application has only one activity, back needs to be like a home button: exit the app but preserve all activity data. My app has a real exit button, where the user shows that really wants to do this.
It's my first app that I developing in mobile world and, for extension, Android world
Some problems seems to me like that it is only possible find the solution if one has a hint about its solution. it's a contradiction. One doesn't know but has to know to solve that don't know!
And, in this situation, it's not the case. No hints. Just question marks.
Before, I had not noticed any pattern. People sometimes act so automatically ... However, suddenly the penny dropped.
I've stumbled in solution. Fortunately!
Not in a million years could I suppose that if someone has an activity and presses Back button, (right button in the bottom), you practically quit the application, even if it remains as a running app for the left button in the bottom (app switcher button)
When I've noticed it, I start to research with another focus. And I've discovered two sources: Disable back button in android and Android - Simulate Home click
So the solution is simply to make the Back button act like the Home button (middle button in the bottom). Return to the home screen without losing application data.
And this is done simply by putting in the onCreate, for my only activity, the following code.
override fun onBackPressed() {
val i = Intent(Intent.ACTION_MAIN)
i.addCategory(Intent.CATEGORY_HOME)
startActivity(i)
}
I'm creating a simple android application to show a website inside the app using WebView. My main activity page asks the user to put the URL of the website and saves it. My next activity is a login page. My plan is to make the login page as the main activity once the user entered a URL so next time they open the app they just need to login.
I did some research and find out that I can go to AndroidManifest to change which activity I want to launch first. but I want the user to put their URL first before changing it. So my question is, is there a way I can put a condition like if statement that checks if URL has data in it, the login page will be launch first?
Create a translucent (or a regular splash if you want) activity which will always be your first activity (launcher activity). This activity will either launch your main activity or login activity depending on whatever condition you code. This way, in your scenario, when launcher activity starts, it will check if url is entered and if it is, it will launch login activity. Else main activity will launch where user can enter url.
I want to execute a piece of code (say, for example, display a Toast) every time that the app is opened. So far I have managed to do this every time the app is launched by putting the code into my MyApp.java file that extends Application.
However, if I press the homescreen or back out of the app and then go into it, the message doesn't reappear. It only does when I relaunch the app. Any idea how to do this?
EDIT:
basically im asking how to execute code everytime the whole APP is brought to foreground (this can be first time open, after another app was used, after user backed out of app, etc). Where would I place onResume code? It wouldn't be in a particular activity, would it, since I want it to apply when entire app appears in foreground, not just particular activity.
You can try writing that code in your activity's #Override-d onResume() method.
The only way to do this is,
Determine which app is currently in the foreground
.Follow this discussion for getting an idea for the best way to do it.
[Determining the current foreground application from a background task or service
Suppose, if the function name is 'getCurrentForgroundApp()',
You need a service to execute getCurrentForgroundApp(); every one second
(1-second interval is depending on your purpose, can be lower or higher).
Now, you can identify which app is running foreground in every second.
So, check if your app is the one running foreground. If true, then execute the toast or code you need.
This is how app-locker apps showing lock screen over selected apps, whenever they come to the foreground.
You have to use the onResume callback:
Android API
Example of use from previous SO question
In activity class:
#Override
protected void onResume() {
super.onResume();
//your code here
}
In my app I have a requirement that if the Home button is pressed during the working of app and if the user starts the app from the icon from home screen it must resume from the last activity.
How do I save the last screen history and resume it from the same screen?
The AlwaysRetainTaskState might be what you're looking for.
In your AndroidManifest, in the activity tag, just add
android:alwaysRetainTaskState="true"
Check out following posts:
Android - restore last viewed Activity,
Saving Android Activity state using Save Instance State.