Back Stack maintain - android - 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>

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.

How to choose starting Activity based on condition in Android

How can I dynamically select the starting Activity of my application?
Take for example this scenario: the first time the user opens the app, he will see the AuthenticationActivity, after that, he will see the MainActivity. If he logs out, he will see the AuthenticationActivity again.
I don't want to use a splash screen or something similar and I'm wondering if there is any way of doing this.
I was thinking of checking if there are any accounts in the onCreate method of the MainActivity, before calling setContentView, and display the AuthenticationActivity if not, but it doesn't feel ok to me.
You could do something like this:
public class NavigatorActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent;
if (isAuthenticated) {
intent = new Intent(NavigatorActivity.this, MainActivity.class);
} else {
intent = new Intent(NavigatorActivity.this,
AuthenticationActivity.class);
}
startActivity(intent);
finish();
}
}
And launch your app with this NavigatorActivity.
You could also set MainActivity the parent of LoginActivity, set LoginActivity as Main/Launcher and call finish() if already logged in.
<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.LoginActivity"
android:label="#string/title_activity_login"
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" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Instead of getting back to the last activity, it launch the parent one.

Up navigation goes to main activity

I have the following Androidmanifest file
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
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=".First"
android:label="#string/title_activity_first"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.anoop.com.myapplication.MainActivity" />
</activity>
<activity
android:name=".Second"
android:label="#string/title_activity_second"
android:parentActivityName=".First" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.anoop.com.myapplication.First" />
</activity>
</application>
In main activity's onCreate method i have the following code to launch Second activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent1=new Intent(this,Second.class);
startActivity(intent1);
}
Other than that every other activities code is what android studio provide as default. But when i press up button from the Second activity it goes directly to main activity and not to the First activity. I know there are alternate solutions for this problem but i just want to know why the up navigation is behaving like this.I am new to android so i may not be understanding something really simple. Any help will be appreciated. Thank you.
If you are using calling SecondActivity in onCreate of FirstActivity, then it goes to SecondActivity. But think about it, when do an Activity actually starts?
It starts after you call onCreate, then onStart , then onResume and then your Actiivity finally starts running. In your case, you are not allowing to go further onCreate, and hence FirstActivity has never even started. And thats why when you go back in navigation it jumps to the previously opened Activity, i.e., MainActivity.
I hope this clears your answer! For reference see the Lificycle of an Activity in this link.
Because FirstActivity is not started or Created. You are calling Second directly from Main Activity. As there is no any First Activity available in application's stack how Second Activity goes to parent Activity First? It will goes to Main Activity as Its the only Activity remain in stack.

Android main activity does not launch on start

The main activity does not launch when I open the application; I close it by clicking on the home button. When the app starts, it opens on the same page where I was when I clicked the home button. It somehow remembers the last page I was at, and opens that page instead of the launcher activity. I added onDestroy() to onPause() on all the activities, but that didn't help either.
I am not sure what code to add here, so I'm attaching my AndroidManifest. Thanks! Below are the three activities in the AndroidManifest.
<activity android:name="example.Activity1" android:label="MyApp"
android:configChanges="orientation" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="example.Activity2"
android:configChanges="orientation"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="example.Activity3"
android:configChanges="orientation"
android:screenOrientation="portrait" >
</activity>
When Home button is pressed, onStop method is called in your activity. So what you may do is to add finish(); in onStop method to destroy your activity.
Your activity is saved on the backstack. Play with the flags passed to the intent when you create your activity. For example, FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET will clear all the activities from this to up the backstack.
Intent myIntent = new Intent(this, MyActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(myintent);
When you close, o exit myintent Activity, when you reopen you app it will clear delete myintent Activity from the backstack and all the activities opened from this activity.
Make sure you activity name includes you entire package name instead of example.
Or just replace android:name="example.Activity1" with android:name=".Activity1"

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