I've been trying to resolve this problem for a few days now but I'm having no luck. Here's the issue.
So I have a service window that opens the in-app billing window, works fine in every scenario except one: When the user closes the screen by pressing the HOME button on the phone then re-opens the app. The window is still open but the app crashes in the background. So, the app crashes then the window is still open, user can still react with it.
When the user presses the app account it's just a black screen, nothing else.
I have a service specifically:
This is created in the Activities "onCreate".
ServiceConnectionToBilling mServiceConnection = new ServiceConnectionToBilling();
bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConnection, Context.BIND_AUTO_CREATE);
mInAppPurchasingEngine = new InAppPurchasingEngine(this, this, mServiceConnection);
So my question is, how do I deal with this? I'v been trying to call the Back Button before the user presses the HOME button (as this closes the service window) but of course overriding the HOME button is just fail on Android so I've no idea how to handle this.
PS I have this:
public void onDestroy()
{
if(mServiceConnection != null)
{
unbindService(mServiceConnection);
}
mGameScreen.DestoryEngine();
super.onDestroy();
finish();
}
StackTrace:
http://pastebin.com/gakrL7qV
I know this error, but how on earth do I deal with it? Only happens when closing the app using home button.
The reason why is happening is because you are not unbinding the service in scenarios where onDestroy is not being called like "Pressing Home", notice that pressing home only calls "onPause" and "onStop", hence, in order to fix this issue you need to do what you are doing in onDestroy (unbind service) during "onPause" and bind again "onResume", so when user press home the unbinding method is properly called, and when the activity is reopened the "onResume" will try to bind the service again...
Regards!
Related
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?
I'm building an Android app specifically a log in page whereby I'd like the app to completely shut down the app if the user was to exit the log in page in any way i.e. using the SWITCH, HOME or BACK buttons.
The only time the app should not completely shut down should be when user successfully logs in i.e. when the custom created log in button or enter button is pressed.
I've been able to do successfully shut down the app but in shuts down even when the user successfully logs. It isn't meant to do this.
Below is my code for the complete shut down - I took over the onDestroy(), onBackPressed(), finish() and onStop() methods:
// Deal with back button
public void onBackPressed() {
System.runFinalizersOnExit(true);
System.exit(0);
}
// Deal with exiting of app
public void finish() {
System.runFinalizersOnExit(true);
System.exit(0);
}
// Deal with exiting of app
public void onDestroy() {
System.runFinalizersOnExit(true);
System.exit(0);
}
// Deal with exiting of app
public void onStop() {
System.runFinalizersOnExit(true);
System.exit(0);
}
DOES ANYONE HAVE ANY IDEAS REGARDING MY ISSUE
YOUR ASSISTANCE IS GREATLY APPRECIATED
Look here for an explanation of the onStop() method. I suppose you open another Activity when the user has successfully logged in, your Activity above becomes invisible and calls onStop() where you exit your app. So just don't override onStop().
The onStop() method is called each time your activity is made invisible: when your app goes to the background, when the screen is turned off, or when you switch to another activity.
Regardless, what you are trying to do is strongly discouraged.
See this forum thread (Dianne Hackborn being one of the lead developer of the Android Framework).
Force closing your app using System.exit(), Process.killProcess() and the likes has a strong risk of conflict with the Android application lifecycle, and can corrupt its saved state, leading to unexpected and unpleasant behaviors for your users.
A better way is to use the flags FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP when launching your login activity.
This should remove previous activities from application, which would be the same as starting a new application from a user point of view.
Just create a class member boolean mLoggedIn and set it to true when the custom created log in button or enter button is pressed.
Then
#Override
protected void onPause()
{
super.onPause();
if (!mLoggedIn)
{
finish();
}
}
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();
}
After my application sleeps, when it resumes I don't want it to go back to the last activity it was on but to start fresh from the main activity as a new fresh start. How can this be done? I also want it to be fresh start in terms not just of being on first activivity but also fresh in start in terms of session etc. How can this be done? Where would I detect the resume and how to go back to start. Thanks
Where would I detect the resume and how to go back to start
You could use events OnResume, OnPause etc. I would suggest you simply finish your app when it goes from foreground:
protected void onPause() { //Another activity comes to the foreground or other apps need memory
super.onPause();
SaveSettings(); //Your own method
finish();
}
An issue has appeared a few days ago.
I have an application that listen for GPS location. The listener is a background service that works all the time, this service saves data in application level and each activity reads this data. So, when i press back button i am able to catch this event and i can stop the service, but when i press HOME button the service is still working although the application is in background mode and this consumes battery, because the GPS always works. How can i handle this event? I do want to stop all services when the user presses HOME button and start them again when user gets back.
10x
It sounds like you're catching the back button either via an onKey... method or in onStop. You should place your code in the onPause() method to ensure it's used whenever the app gets backgrounded.
You can not handle home button events in your Android application. Google has made it for internal use only.
LINK 1
LINK 2
#Override
protected void onUserLeaveHint()
{
// When user presses home page
Log.v(TAG, "Home Button Pressed");
super.onUserLeaveHint();
}
You could create a OnKeyListener, and check if the pressed key was Home. I never tried it, though.
create a custom activity called customActivity extends Activity now override method(in customActivity) to catch home button event and stop your service(create & start service in application class). Now extends customActivity instead of Activity for any activity_class.
Long press the HOME button, it will enlist the running process, select the one you want and then exit it gracefully.
OR
From Home -> settings -> application -> manage application
you can kill the unwanted process.