Android recent apps - android

I have a strange problem with my app. On my HTC One X max apps in recent apps is 9 and if my app gets pushed out of these 9 apps when I start it again it acts weird and breaks in all kinds of ways.
In my BaseActivity OnRestart I have code that switches current activity to main activity. This doesn't happen in above scenario.
protected override void OnRestart()
{
base.OnRestart();
if(someTimeHasPassed)
{
var intent = new Intent(this, typeof(MainActivity));
// Kills every open activity to disable user from going back.
finishAllOpenActivities();
StartActivity(intent);
}
}
I'm not sure what part of code to post. Anyone had any similar issues?
EDIT: After the above scenario happens
Window.SetFlags(WindowManagerFlags.Secure, WindowManagerFlags.Secure);
in OnCreate() never executes because when I press home button I can see my app normally, no black screen. And by normally I can see the background and default header with activity name, nothing else gets loaded.
Not sure why OnCreate is even being called because I'm returning to that screen.
EDIT 2: After more examination, this seems to happen when some apps are in front of mine in recent apps. Could this be a memory issue?

Related

Close all Activities when the app returns from being in the background

It is important in my app that when a user presses the home button and the app enters the "Background" the app reverts to the login screen next time it is opened.
I have tried using the
android:noHistory
in the manifest but this means when pressing back on activities it reverts also to the login screen so is no good. I have also tried using
android:clearTaskOnLaunch="true"
in my initial activity but this doesn't seem to have any effect. I don't finish() the first activity so i am not sure why this doesn't work.
Can anyone help with a way of doing this.
I got this working by using
android:clearTaskOnLaunch="true"
Turns out that I didn't have it in my root activity, this was because I have a second login screen which asks for less details depending on if you have logged in before.
Although it works exactly as i want it to, apparently using this "Android" feature is incorrect because i am going "against" android by clearing the tasks (activities) when the home button is pressed and my app should follow someone else opinion of what an "app" should do. Who Knew!
Why don't you use the onPause()-Method? This should be called every time the Activity gets in Background.
So something like:
#Override
public void onPause(){
super.onPause();
finish();
}
Due to the fact that only one Activity of your App can be active at a time you don't have to worry about others still being active.

Start Android App on same activity every time

I'm working on an Android app that will show college fitness professors how their students are doing in their classes. Since this data is fairly sensitive (biometrics are shown, including weight, something many college students are self-conscious about) I don't want the data to be available to anyone who picks up the tablet. While I have a proper login screen created, complete with authentication for the database, etc. I have an issue when the home button is pressed. Since Android doesn't close a program immediately on leaving the app, it's possible to reopen it and return to where you were. I would like to force the app to return to the login screen each time (I've altered onBackPressed so you can't just return to the previous view from the login screen) so that you have to re-enter your credentials to get back into the app. However, I can't seem to do this. An answer I found on here said to use the following line:
android:clearTaskOnLaunch="true"
However, no matter what XML file I put it in, be it the Manifest or the individual Activity XMLs, it appears to do nothing. So, how do I ensure the login screen comes up each time the app is launched, regardless of whether it is starting from scratch or not?
Try to play around with onUserLeaveHint() method. If you read its documentation, it says:
Its Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called
So, when ever you detected home button pressed, you can finish the running activity/activities. So next time user click the app, it will start from the first login screen.
Hope this helps.
You should override onUserLeaveHint()
#Override
protected void onUserLeaveHint() {
// do your logic
//finish() or start login activity
}
You could set a flag when onPause() is initiated within the activity. And then when you return you could check the flag from within onResume() and then request a login from that point. This will be sure to request it each time; in a simple case of course.
Edit:
With multiple activities, you could check against a saved context to see if they are the same when you start a new activity. If the contexts differ then you can discard the context previous activities context and start a new activity.
If they are the same, then you have come back to the activity from itself (you have lowered and brought the screen back). You would have to use some form of saved state such as that to do it in this manner with multiple activities when outside the case of a simple application.
I found out how to do it in my case. For any others with the same problem, try following the example here:
Android detecting if an application entered the background

Activity not closing properly

I am calling one activity on click of status bar notification which is having a Complete button. on click of btn. i have folllowing code -
public void completeTask(){
taskDBAdapter.deleteReminder(rowId);
taskDBAdapter.close();
Intent intent = new Intent(this, TaskManagerActivity.class);
startActivity(intent);
finish();
}
whhen i click complete btn new activity (TaskManagerActivity) gets opened properly.But if i reopen my application it still tries to open this activity and not my default landing activity. Any help on this.??
EDIT -
I have tried repositioning my finish() statement . Still its not working.
EDIT 1.1 -
Ok I will provide some details here. Assume my app has two activities
Main Activity
Notification Activity
My app create some notification to display on Status bar. So as soon as i click on status bar Notification actvty will open. Now there is a button called Complete on click of which the code given will fire and main activity (in the code TaskManagerActivity.class) will open. But after I press back button in my app and again reopen it , it opens the notification activity when it should have fired the main activity (as it is launching activity).
Thanks,
Ray
That's the default way android functions. If you press the home button and then open your app again, it will restore the apps previous state (unless it has killed the apps processes and activities due to memory constraints). So you are not actually restarting your app but only restoring it.
If you wanna quit the app, then press the back button. Now when you re-open the app, the original activity will be launched.
Do not modify this behavior. It is the default system behavior and users expect it to work this way. Your app is fine :-)
- First of all the behavior which you are experiencing is the way Android is made to function, moreover when a user gets a call while this app is open, after finishing the app he will definitely want to get back to the state where he left the application.
Still if you want it that way, here it is.....
- Make sure your application has only single instance of it running, by using android:launchMode="singleTask", or android:launchMode="singleInstance"
- Then finish() your Activity at onPause().
#Override
void onPause()
{
super.onPause();
finish();
}

Displaying app main menu/home screen when returning to app after having started an external Activity/Intent

I'm starting Android Market via my app to search for similar products using this code:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://market.android.com/search?q=pub:\"some txt\""));
c.startActivity(intent);
This works fine for showing similar products. However, if I hit the home button while in the market, when I re-open the app it still shows market results. I want to go to the main menu in this case.
Is there a solution?
Sorry, FLAG_ACTIVITY_NO_HISTORY is probably not the correct solution. Note the semantics of it -- the activity just doesn't appear in the history. Thus if the user taps on one of the things in it to go to the next activity, then pressing back, they will not return to the previous one (but the one before). This is rarely what you want.
Worse, if they go to a second activity from the market activity, press home, and return to your app, the second activity will still be there (it is keeping itself in the history).
The correct flag for this situation is FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET.
If you add the FLAG_ACTIVITY_NO_HISTORY flag to the intent, it won't be kept on the history stack. When the user navigates back to your application, the last activity that was visible before you launched the marketplace will be shown.
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://market.android.com/search?q=pub:\"some txt\""));
c.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
c.startActivity(intent);
Edit: hackbod is correct: FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET is a better fit for what you need.
This is not a problem.
When you press home on the Market app it isn't closed, just paused. So when you open it again you resume it. Check Android activity's lifecycle.

Android Run application from last Activity

I have an application with several Activities.
My A Activity has the Manifest Intent filter parameters: action.MAIN
and category.LAUNCHER. after its being loaded I call Activity B and
finish() A since I don't use it anymore.
After I run my application, go from Activity A to B and press the Home
button, when I relaunch it from the applications menu or from the
Market app for ex.(not by a long press on the Home button), starts
from the A Activity and do not save its last Activity B.
I definitely know that this is possible to relaunch an application
from its last Activity (some applications from the Market do support
it) and I think that this can be determined by the Manifest parameters
but I don't know which one.
does anyone know how to implement it so my application can relaunch
from its last Activity B?
Thanks
ayanir
Though I know this is an old question, I was struggling with this very issue and couldn't find an answer on SO. So, here is my (very newbie) answer:
No, I do not think it's possible to do this by messing with the manifest - you can only launch one fixed activity per app from the home screen. What you can do, though, is launch whatever activity you want from that starting point, and Android can do it quickly enough that you never see the first one.
Though this feels very much like a hack, I implemented this routing in the starting activity's onResume() method, and used sharedPreferences to keep track of which activity to launch:
final Class<? extends Activity> activityClass;
SharedPreferences prefs = getSharedPreferences("sharedPrefs", MODE_PRIVATE);
int activityID = prefs.getInt("whichActivity", -1);
if (activityID == Constants.ACTIVITY_ID_MAINSCREEN) {
activityClass = MainScreen.class;
} else {
activityClass = null; return;
}
Intent newActivity = new Intent(this, activityClass);
this.startActivity(newActivity);
There have been a number of very similar questions lately. It's a good idea to search the site first to ensure that duplicate questions don't get asked.
For example, the question linked below says that this behaviour was happening because the developer was starting their application using the Eclipse debugger. Another person was having this problem because they were launching the application directly from Eclipse, rather than starting cleanly by manually pressing the launcher icon.
Android: keep task's activity stack after restart from HOME
so there are a few things to consider when developing Apps in Android. And one of the big things is the Application Lifecyle, if you haven't yet then I would suggest this video. What happens is that an application can be killed and reset at any point in time and you need to save the state of your application so that you can restore it at any time. If you open your App from the Launcher you will always go into the Activity that starts the app, if you want to skip to the next Activity you need to store that information and then jump to the Activity in your code.
Also have a look at this documentation about SavingPersistentState

Categories

Resources