Android app: managing activities - android

I am trying to learn android programming. I have made a basic app. It has a main activity, a settings activity and a notification that opens the settings activity on click. Main activity also has a settings button that opens settings activity. It may sound weird but i am trying to learn only.
So the problem is when i open the settings activity and then click notification, it again opens settings activity on each click on notification. Then when i hit back button on my phone, it takes me again to settings activity and i have to press back button as many times as i clicked the notification to go back to main activity. Looks like the settings activity is being instantiated again and again on each click on notification. I have tried using many things i read online like trying some Intent flags but can't find anything working. I tried using finish(); in onBackPressed in my settings activity but doing this takes me to main activity and when i hit back on main activity, it takes me to settings and then again i have to hit back that number of times.
How do i make the notification open the settings activity only if its not running?

I think this can help you.
Edit in AndroidManifest.xml
<activity android:name=".SettingActivity" android:launchMode="singleTask">
This will open the activity if its not running.

Related

How to configure the back stack when activity is launched by BroadcastReceiver?

I have an app with MainActivity.java which has a button that when clicked launches Main2Activity.java. Main2Activity.java is added to the project in Android Studio using New/Activity/Basic Activity. It has a back arrow at the top and touching it will return the app to MainActivity. AndroidManifest.xml has the following for Main2Activity:
android:parentActivityName=".MainActivity"
The app also has a BroadcastReceiver which, after performing its work, launches Main2Activity.java. When started this way (when the app is in the background), the back arrow in Main2Activity does not go back to MainActivity but instead exits the app.
I would like the back function of Main2Activity to always go to MainActivity. According to Navigate up with a new back stack, there seems a way to do this, but the example is meant for an activity launched by another app.
How do I set the back destination for an activity launced by a BroadcastReceiver?
There are two ways of doing this:
Providing the Up Navigation as mentioned in the link quoted by you in the question. It should work even when you're navigating to the Activity through a broadcast.
Use startActivities from the BroadcastReceiver to start both MainActivity and Main2Activity. This allows you to create a backstack when starting an Activity and it's parent is not on the stack.
I prefer approach 2 simply because it works even when you press the hardware back, not just the back button on the Action Bar. For achieving that with approach 1, you'd also have to override onBackPressed.

Android app navigates to launch activity when coming from background

I have 3 activities ( say A, B, C were A-is the Launch activity). When I press home button when at activity C, app goes into background. After that, I took my app through all apps list menu. At that time, my launch activity is showing (activity A). When I press back button, It goes to previous activity (C). I want to retain in the same activity (C) while coming back from background. When I run application through Eclipse, It works fine. But when I send Apk file through mail and run it in device, It fails (previous problem occurs ).
I tried with
android:launchMode="standard"
AND
android:alwaysRetainTaskState="true"
in my launch activity (Login activity or A). Any body please help me, Thanks in advance.
Follow following steps to insure that you are following the right practice:
1. Make sure you are using finish(); on the Activity A or B if you want to finish it and dont if you want the back button functionality.
2. Try Implementing onpause() and onresume() even if you are not going to perform any functionality in them. Use super() for them there.
3. Also, in android when you start an activity by clicking on the icon instead of resuming it from already running activities, it exhibits a different behaviour.

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();
}

Home key will not open last activity

When i press Home key and then application Icon then it will show me last activity but it will always start the first activity that is from main->first activity.Where main is the launch activity.But if i restart the application then it will work perfectly.So can anybody know why it will work after restart and not at starting?
My Application flow is like this way.Application class->Main activity->Tab activity with5 tabs and each tab open separete activity.edit I restart application like this way: in eclipse again run the application and in phone from settings->application->manage applications->myapplication->force close.Then click on application icon. Sipdroid is launch activity and welcome is first activity which has 5 tabs.
This is my menifest.xml file
Its solved by just removing the android:launchmode="SingleInstance" tag from manifest file.

Relaunching Activity again?

I have three activities A->B->C.when i am in B activity i click the home button and put the application to back ground(B->home).
When clicking on the my application icon in the home screen the Activity A is opened not activity B .but upon long pressing the home key,its showing that my app is running in background and clicking the icon open the Activity B as expected.
What was the problem?i didn't handle any home key event.How to prevent relaunching my application.
When you make a long press the dialog shows recently used app and not the ones ruuning at the moment. To make sure your activity is not killed go toSettings-> Applications-> Manage Applications -> Running tab.
Usually the behavior you described is cause by launchMode attribute in the manifest.
Check that you do not have anything there.
read more about "launchMode" attribute here.

Categories

Resources