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

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.

Related

Up navigation using item of a navigation drawer menu

In my android app I have two activities, one is the LoginInActivity (i.e. MainActivity), and the other is the HomeActivity which is a child of the first activity and is implemented with a Navigation drawer. So in my manifest file I have:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeActivity"
android:label="#string/title_activity_home"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
Obvioulsy in my MainActivity I create an Intent and start the HomeActivity with startActivity() method.
Now I would like to use an item of the Navigation Drawer as if it were a Up Button, which means that I can navigate back into the hierarchy and log out.
Furthermore, I want to add more children to HomeActivity later, which is way I did not use startActivityForResult() method in MainActivity and I didn't call finish() in HomeActivity.
I'm wondering if it's possible to implement this thing. If so, how? Otherwise it is more advisable to let the HomeActivity (with Navigation drawer) become the Main, and that the LogIn is a drawer item (e.g. calls an LoginActivity with startActivityForResult())?
You can navigate back by calling onBackPressed() in your click event.

Back Stack maintain - android

I developed one application in android.It works perfectly but if i press back button then it will show all the previous activities like currently login,previous login etc. means all the things. How this remove from?
Use finish() after Intent calling for next activity.
for example..
Intent intent = new Intent(YourActivity.this, NextActivity.class);
startActivity(intent);
finish();
It open next activity and remove activity from BackStackTrace.
call finish() before starting the next activity
call finish() after Intent calling for next activity.
/
Providing Proper Back Navigation
https://developer.android.com/training/implementing-navigation/temporal.html
For example:
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- The meta-data element is needed for versions lower than 4.1 -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>

using actionBar.setDisplayHomeAsUpEnabled in xml

I am writing this app and I seem to be calling actionBar.setDisplayHomeAsUpEnabled(true) in every Activity I create. I'm wondering if there is a way to set this value in the styles.xml or theme.xml; rather than repeating the same line of code every time.
Another way is you can declare in your AndroidManifest.xml
<activity
android:name=".CurrentActivity"
android:label="#string/title_activity_detailed_forecast"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.youpackage.name.appname.MainActivity" />
</activity>
CurrentActivity is your activity that you are in and MainActivity is your previous activity

Navigation Back to previous activity in 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()

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

Categories

Resources