all activity reverse when press back button - android

In my project I create several class and I am able to going from one activity to other , but the problem is when I press back menu all activity reversed I don't want that!!! below is manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bones.samples.android"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name" android:icon="#drawable/icon48x48" android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<activity android:label="#string/app_name" android:screenOrientation="landscape" android:name=".stage">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="LevelOne" android:screenOrientation="landscape" ></activity>
<activity android:name="dialogue" android:theme="#android:style/Theme.Dialog" ></activity>
<activity android:name="FailDialogue" android:theme="#android:style/Theme.Dialog" ></activity>
<activity android:name="LevelTwo" android:screenOrientation="landscape" ></activity>
<activity android:name="LevelThree" android:screenOrientation="landscape" ></activity>
</application>
<uses-sdk android:minSdkVersion="10" />
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens= "true"
android:anyDensity="true"
/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>
</manifest>
I want that when I am on leveltwo or dilogeue activity and press back button it directly go to stage activity but not in reverse all activities.

If you call one activity after another without calling finish, the activities are preserved in the back stack for further access.
If you dont want the activities to show up when the user presses back, keep calling finish method after calling the activity you want.
For Ex:-
Intent intent =new Intenet(this, NextActivity.class);
startActivity(intent);
finish();
This will close the current activity and will start the next activity. Now, when the user presses back, it will go the the activity earlier than this activity else, your application will close.
If you want to go to a specific activity, you can override the onBackPressed method and call a specific activity too.

Use:
YourActivity.this.finish()
after calling the intent to next activity, for all activities you dont want to display on BackPress.
if the above is not feasible, you can define the parent activity for up navigation:
Beginning in Android 4.1 (API level 16), you can declare the logical parent of each activity by specifying the android:parentActivityName attribute in the element.
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
This is what the Docs say

startActivity(CurrentActivity.this,NextActivity.class);
finish();
or
set flag on your manifest file
read below thread for more info
http://developer.android.com/guide/components/tasks-and-back-stack.html
or
Use fragment. u can replace the fragment . finally u can finish the activity.

Use this into Manifest...
android:noHistory="true"
as below...
<activity android:name="LevelOne" android:screenOrientation="landscape"
android:noHistory="true" ></activity>
<activity android:name="dialogue" android:theme="#android:style/Theme.Dialog"
android:noHistory="true" ></activity>
<activity android:name="FailDialogue" android:theme="#android:style/Theme.Dialog"
android:noHistory="true" ></activity>
This attribute will remove the current Activity from the Activity Stack when another Activity will start from this Activity...So, when you will press back button then it will not return to that previous Activity.

Related

Android - Back stack is not maintained when app is moved to background and bring to foreground

I am having 3 activities in my app and the flow is like A->B->C. When I was in activity C, I moved the app to background by pressing the home button. I brought the app to foreground again and it showed activity C as expected. When I press back key, it's closing the app and B and A are not shown.
I put the launch mode for A, C as single instance and for B, nothing is specified. I want to have one instance for all my activities. I have tried by changing the launch mode to single task(with permutation and combinations of all activities) and it didn't work.
Here is my manifest looks like for activity declaration
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
tools:ignore="GoogleAppIndexingWarning"
android:roundIcon="#mipmap/ic_myway_circular_launcher"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar">
<activity
android:name="A"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="B"
android:windowSoftInputMode="adjustPan" />
<activity
android:name="C"
android:launchMode="singleInstance"
android:windowSoftInputMode="adjustPan"/>
</application>
Can someone tell me how to maintain the back stack always even when the app is opened from background?
Thanks in advance.
you can specify the parent(previous Activity) of any activity in the Manifest
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
tools:ignore="GoogleAppIndexingWarning"
android:roundIcon="#mipmap/ic_myway_circular_launcher"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar">
<activity
android:name="A"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="B"
android:windowSoftInputMode="adjustPan" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".A"/>
</activity>
<activity
android:name="C"
android:launchMode="singleInstance"
android:windowSoftInputMode="adjustPan">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".B"/>
</activity>
</application>
After a lot of research, I found that it's the problem with the launch mode. As per Android documentation, for this mode, the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task
But I wonder the issue is only coming when the app brought to foreground from background. If it is continuously running on foreground, issue is not there.
Finally, I changed it to default mode and it's working now.

When I charge my app after using home button it starts in my first activity

I'm sure that this will be a really newbie question but I'm stuck and I don't know how to get out of this!
I have an app that have three activities and when I use HOME button and I open the app again, it goes to the FIRST activity always, even if I was in the second or at the third one.
EDIT 3: My app comes in three activities, the first one is the main menu, the second is a map of tables and the third one are the data of the tables. Depending of the configuration, closing the third activity must bring me to the first one or the second one, and when I'm leaving the third Activity I dont want it to stay on the Activities stack. My program is working fine going from an Activity to another one. My problem is that seems that when I use Home Button my app finishes every Activity except the first one.
Maybe I have to modify anything on the manifest or maybe I have to use in a specifically way the RestoreInstanceState but I'm searching so hard and I can't find anything. Thanks in advance!
Edit 1: I'm adding my 'application' xml part of the Manifest:
<application
android:allowBackup="true"
android:icon="#drawable/icobaccus"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.example.tpv2_tablet.Activity_Start"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|keyboard"
android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.tpv2_tablet.Activity_Zonas"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|keyboard"
android:theme="#android:style/Theme.NoTitleBar">
</activity>
<activity
android:name="com.example.tpv2_tablet.PrintDialogActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|keyboard"
android:theme="#android:style/Theme.NoTitleBar">
</activity>
<activity
android:name="com.example.tpv2_tablet.Activity_Mesas"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|keyboard">
</activity>
</application>
Edit 2: Maybe I'm doing something wrong when calling other activities or I'm calling a wrong flag:
Intent intent = new Intent(Activity_1.this, Activity_2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
Remove
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
from all activities except launcher activity
To understand the reason why, read Google Documentation here
remove:
android:noHistory="true"
As this will remove the activity from the activity stack
remove:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
from:
Intent intent = new Intent(Activity_1.this, Activity_2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
to understand the reason check this answer
You need to set android:clearTaskOnlaunch="false" in your android manifest.

Android parent activity

I have this in my manifest:
<activity
android:name=".PimMediaActivity"
android:hardwareAccelerated="true"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".SettingsActivity" />
</activity>
On my PimMediaActivity which has SettingsActivity declared as its parent in the manifest, I have a custom back button. There are cases like from push notification where I need to start the PimMediaActivity directly, but I want to be able to go back to the parent activity when the button is clicked. Is this feature only working with the ActionBar back button ?!
You should put this :
<activity
android:name=".PimMediaActivity"
android:hardwareAccelerated="true"
android:screenOrientation="portrait"
android:parentActivityName=".activities.SettingsActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".SettingsActivity" />
</activity>
Are you interested in retrieving the app state from before opening notification?
If no, you can just call startActivity(Intent) in onClick() method..

Android app always opens the first activity on resuming

I'm working on an application that currently has two activites, a log in activity and a "frontpage" activity after that.
Whenever I switch away from the application and resume it either by relaunching it or using the application switcher, it will always re-open the log in activity. Even if I switch to my app from within the frontpage activity, it opens the log in activity.
None of my other apps have ever done this, even though they obviously all have a filter for the launcher in AndroidManifest.xml. I am aware that it is supposed to do this when the activity is killed, but I cannot comprehend why it would do that.
This is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.vertinode.facepunch"
android:versionCode="1"
android:versionName="1.0">
<!-- Android 2.2 -->
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
<!-- Internet access -->
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="#drawable/icon" android:label="#string/app_name" android:name=".FPApp">
<!-- Login form -->
<activity android:name="nl.vertinode.facepunch.LoginActivity" android:launchMode="singleTask" android:theme="#android:style/Theme.Light.NoTitleBar" android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Frontpage view -->
<activity android:name="nl.vertinode.facepunch.FrontpageActivity" android:theme="#android:style/Theme.Light.NoTitleBar" android:windowSoftInputMode="stateHidden">
</activity>
</application>
</manifest>
What could I be doing wrong?
I'm going to guess this has to do with your android:launchMode being singleTask.
Try singleTop instead and see if you have the same problem. Also it could have to do with some code you have in your onPause for the other activities. Does it go back to your Login activity after going to sleep/waking back up as well?

androidManifest and intent-filter to order activity

I have 3 Activity in my androidManifest,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="zepod.whatelsecomics" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Activity1"
android:label="#string/list1">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity2"
android:label="#string/list2">
</activity>
<activity android:name=".Activity3"
android:label="#string/addItemToList2">
</activity>
</application>
The first one call the second one
The second one call the third one
The first one activity is the main activity.
The result of the third one activity is an updated list in the second one (just a form).
But now, if i clik the back button i came back at the third one
How can i force the app to came back at the first activity?
I suppose this depend by androidManifest but i don't understand the intent-filters order
Can someone help me?
When starting to the third activity, call finish() to remove the second activity from the stack. That's the easiest way.
There are other ways. This question has been asked a million times on SO, search for it and you'll find alternatives.

Categories

Resources