Navigation Back to previous activity in android - android

I am trying to navigate back to previous activity with the UP back button in ActionBar but it doesn't work properly. I have
getActionBar().setDisplayHomeAsUpEnabled(true);
in
onCreate()
method and in
onOptionsItemSelected
I have
if(item.getItemId()==android.R.id.home){
onBackPressed();
}
On emulator it works fine. When i install the application on my mobile, then the back button is gone.
Any ideas how to fix that issue?
Part of my Manifest:
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<activity
android:name="datepicker.DatePicker"
android:label="#string/title_activity_date_picker" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
<activity
android:name="gridviewcontext.TableItem"
android:label="#string/title_activity_table_item" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>

You have to add the parent activity name in the activity you enabled the back button in your manifest:
android:parentActivityName="your_parent_activity"

I am assuming that you are testing the app in a mobile with version less than API level 16.
If your app supports Android 4.0 and lower, include the Support Library with your app and add a element inside the <activity>. Then specify the parent activity as the value for android.support.PARENT_ACTIVITY, matching the android:parentActivityName attribute.Do as follows,
<activity
android:name="myomega.datepicker.DatePicker"
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>
in your manifest file, you are missing android:parentActivityName="com.example.myfirstapp.MainActivity" this line.
refererence http://developer.android.com/training/implementing-navigation/ancestral.html

Use finish() instead of onBackPressed()

Related

back button close the activity instead of going back to main activity

in AndroidManifest.xml I put this
<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>
In onCreate() method I put this. I didn't override onOptionsItemSelected.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
The problem is when I press the Back button(on the action bar) The activity get closed instead of going back! Help me to resolve this. Why I go for this method is I can easily place a back button on the left.

Android Lollipop calls onCreate after finish()

This is a strange behavior of Android Lollipop 5.0.
I have two activities, A and B.
A starts the activity B. When I click on the back button, in activity B, Android calls onCreate method on A.
This behavior is observable only in Lollipop 5.0.
In the other versions, onCreate is never called after finishing another activity.
What is the problem?
This is my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example"
android:versionCode="1"
android:versionName="1" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="21" />
<application
android:name=".Application"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppThemeMaterial" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".activities.ActivityA"
android:configChanges="orientation|keyboardHidden|screenSize"
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=".activities.ActivityB"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/activityB" >
</activity>
</application>
</manifest>
please make sure that option not selected
Developer Options -> Don't keep activities
What I think is happening is that when you leave the Activity A, onStop() is being called on activity A since its completely hidden and B is on top of it.
Usually now when you resume activity A after pressing back onStart() is called and then onResume().
But, if you see the Activity LifeCycle, it is technically possible for onCreate() to be called as well, in the case where your app's process is killed by the system if other apps with higher priority need more memory.

all activity reverse when press back button

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.

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..

configChanges not recognized by TabHost?

I have a pretty big problem with my TabHost. Although I have declared all of my Activities (Including my TabHost Activity) to ignore orientation and keyboardHidden, if I flip my Phone (Android 4.0) It still recreates the activity.
Here is a shortened version of my Manifest:
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".TabHost"
android:configChanges="orientation|keyboardHidden"
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=".Activity1"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name" />
<activity
android:name=".Activity2"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name" />
<activity
android:name=".Activity3"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name" />
</application>
I don't think that the code of the activities would help?!
If it would however, let me know and I'll post it too.
Kind regards
if you are trying to stop the rotation of the screen use following code in your AndroidManifest.xml for each activity.
android:screenOrientation="nosensor"
UPDATE
According to API when using android:configChanges="orientation|keyboardHidden"
"orientation"
The screen orientation has changed — the user has rotated the device.
Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.
so, if you are targeting API level 13 or higher you gotta specify screen size also.

Categories

Resources