Differences between up and back (in programming) - android

I want to know what is the differences between the back and up actions.
I've already found online how they work, but I didn't find what different happens when I trigger them.
When I intent another activity and go back with up action, it seems to just show again my last activity without executing any method (my images show like it was cached).
When I intent another activity and go back with back action, it seems to reconstruct the entire layout (my images reloads).
What really happens?
Thanks.

First you must understand the concept of Up and Back navigation, you should read this link about Navigation with UP and Back
When pressed back button the actual screen is removed but when pressed up button relaunch the parent-activity.
In your manifest need to add propert launchMode of activity, something like this
<activity
android:name="com.example.client.app.MainActivity"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I hope you serve.

Related

SingleTask activity is created multiple times within the same process

I have a launcher app which has a singleTask activity as the main entry point. When the user navigates away to another activity or to a 3rd party app and then hits the home button, then this activity should be brought to the front. However what I experience is that for the first home button press only, another instance is created instead (a new task is created, onCreate() is called). In the meantime the old task is still alive, containing the original instance of this activity, but it is impossible to navigate back to that task/activity or to bring it to the foreground.
After the first home button press, the next home button press brings the 2nd instance of this activity to the foreground. Not sure why not the very first instance's onNewIntent() method is called for the first time... So this only happens once, after that always the 2nd instance's onNewIntent() method is called. This means that the original activity will be not accessible..
I tried to bring the task to the foreground, nothing was happening... Like if it never existed (but the task is there with the activity, it is not killed at any point). I can find the task from code and also using a shell script. It contains the original activity
This is happening on Android TV (Os: Pie). Any idea what can be the reason for this? I do not really understand how this is happening... BTW the result is the same if I set the activity to singleInstance.
The activity looks like this:
<activity
android:name=".activities.MainActivity"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:theme="#style/AppTheme">
<intent-filter android:priority="2">
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ALL_APPS"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
I tried alternating the above attributes (priority, excludeFromRecents, taskAffinity), also removed them completely, they had no effect...
The home button press sends the following intent:
action: "android.intent.action.MAIN"
category:
"android.intent.category.HOME"
component: the above activity
it has also some extras but I do not think it is relevant
You are experiencing this nasty long-standing Android bug:
Re-launch of Activity on Home button, but...only the first time
You say in a comment that it doesn't happen with real users. That is not actually true. If a real user would install your app from the Play store and then launch it immediately (click the OPEN APP button after installation) then the problem would be reproduced exactly as you describe it.
I was struggling with the exactly same issue for the last couple of hours and I've read many similar Q&A about this topic/bug on StackOverflow. No solution really worked for me until, out of mere curiosity, I did the following.
If you're building a launcher and using onNewIntent() in your MainActivity (although OP doesn't mention that he's using this method), then simply comment out this line:
super.onNewIntent(intent);
Uninstall your app and install it again.
I don't know how this works, but EVEN when you then uncomment the very same line, it still behaves in a proper manner, that is: the app has really only single instance of its main activity all the time. And how I know that? My MainActivity is doing some database operations, and previously I saw in my Logcat that these operations were done twice every time, but now they are done only once.
And BTW my MainActivity tag in AndroidManifest.xml is nothing special:
<activity
android:name=".activities.MainActivity"
android:exported="true"
android:excludeFromRecents="true"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Android - start the same activity after starting the app

My app consists of 2 activities:
<activity
android:name="com.domain.android.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.domain.android.AboutActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I open my app, navigate to second screen, then go to the homescreen. From there I launch the app again - and it starts the main activity. But the app is running, it is just in the background, but why it didn't open the second activity? I guess it is something connected with activity's category. But how to fix that? Thanks.
Welcome to the ever-growing list of developers who have been bitten by this Android bug. Please see Re-launch of Activity on Home button, but...only the first time for all the gory details. And please go to Google Code and star the issues. We need all the noise we can make to get these fixed.
Hold your home button it will show the running apps click on your app it will open the activity from where you left. even if you click on the application launcher it will open the activity from where you left.
your application will not be in the same state sometimes if android needs resources it might end that activity. open an application move to next screen and press the home button and launch again it will open from where you left do the same with 5 or 6 apps then try launching the 1st app it will not be in the same state it will launch from the launch activity but any app you come to home screen and launch immediately it will open from where you left. If the background app is not doing anything android might end it if it needs resource. correct me if i am wrong. additional information i will be happy to know.
One issue may be that you have 2 activities designated as the main activity using:
<action android:name="android.intent.action.MAIN" />
You will probably have two icons in your launcher for your app. Each one will launch a different activity. You might be launching the first one again and again by using the icon for the first activity. Try removing
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
from your AboutActivity activity declaration. This might fix your problem.

Android how to open Index activity always

Today got problem that my apps cannot go Index activity always whenever I've already configured in manifest file.
<activity
android:name="com.ppshein.inm.IndexActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Problem is, I have two activities (index and detail).
1) open my apps, it go Index activity.
2) Click button in Index activity, then go Detail activity.
3) Click android "Home" button to exit in Detail activity.
(not click on android "back" button).
4) Once again open my apps, it automatically go to Detail activity (that's I don't want).
When I want is whenever I open my apps, always want to go Index activity first even close at Detail activity.
Nope, I've found the answer. The right answer is to add following attribute into the activity we want to set default as always.
android:clearTaskOnLaunch="true"

android "close" activity

i have an application that has "login" and "logoff" actions. after the user logs in, there could ne an arbitrary number of activities left on the stack at the point when they log off.
when the user logs off, i want all application state to be reset. i clear my disk caches and preferences, but there's still state left in running (paused) activities. in fact, when the user logs in again, i find that the old activities from the previous login session are still there with their old state (i understand why this is of course). how can i cause the entire app to reset / close / restart?
i've seen a few posts about using startActivityForResult(), onActivityResult() to bubble up a "logoff" result code causing each activity to close itself in turn. this looks painful and i'm hoping there's a better way.
EDIT: i've tried starting the "log in" activity with various flags to no avail,
loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
on your manifest you can put the following attribute to your activity:
android:clearTaskOnLaunch="true"
so you would end up with something like:
<activity android:name=".ui.Activity" android:clearTaskOnLaunch="true" android:theme="#style/MyTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

How could I set an activity to be the one to be resumed (by multitask)

I am experiencing a little pain on Android. I have two activities, one is a Splash Screen, launched when you click on the app icon, and an other one which can wake up when a specific event occurred (a NFC event, but it is not the problem).
There is the declaration in the manifest :
<activity android:label="#string/general.appName" android:name=".activity.SplashActivity" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:excludeFromRecents="true" android:label="#string/general.appName" android:name=".EventReceiver" android:noHistory="true" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.nfc.action.TRANSACTION_DETECTED">
</action>
<category android:name="android.intent.category.DEFAULT">
</category>
<data android:host="secure" android:path="/aXXXXX" android:port="0" android:scheme="nfc"/>
</intent-filter>
</activity>
All works fine, it means that the EventReceiver is launched when it received a specific event, the Splash Screen is functional etc...
But, when the application is launched by an event, so it is the EventReceiver which is launched first. If I pause the app by clicking on the home button of the phone, and by long clicking on it again, it makes appear the multitask table, with all the recent opened apps. There, is I click on my application, it resumed the application on the EventReceiver, just like if an event had occurred. So do you know a way to resume an application on one selected activity only (in my case the SplashScreen).
Or do you know a way to recognize that the application was resumed by a multitask function ? (I could open the good activity at the onCreate of my EventReceiver too if I get this information).
For the moment, the only solution I found is to put a android:excludeFromRecents="true" flag on my EventReceiver activity. This will result that the application will not appear in the multitask table if the application was launched by an event. But this is a little bit tricky and I do not like that very much.
Do you have any ideas ?
Thank you very much beforehand for your answers ! (and sorry for my scholar English ^^)
If you really have to detect when the application is lauched from the "recents" you can use intent flags.
if (getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) {
// Lauched from recent apps.
}
Do NOT use a splash screen. It only slows down your app experience and has no value what so ever.
In addition... Android apps are a lose set of activities... There is no concrete entry point that would need a splash screen.
That said... the app switcher returns you to the previously active activity. In your case the .EventReceiver. You could change your structure by using a second activity that shows your ui so that the EventReceiver lets you forward to that.
Correct me if i am mistaken... But can't you intercept the nfc event with an receiver? From there you should open your ui activity.

Categories

Resources