Parent activity keeps reseting when navigating back from child - android

When I use an intent to move from parent activity to another, and then use action bar nav to go back to parent, the parent activity has lost all my changes and values (as a user) and is back to default xml display i have for it.
I have these in my Child activity OnCreate:
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
And labeled my parent activity appropiately:
android:parentActivityName=".MainActivity"
What do I need to make it store or remember those changes when traveling between activities?

In the Manifest file, you should set android:launchMode like this:
<!-- ACTIVITY -->
<activity
android:name=".activities.MainActivity"
android:configChanges="orientation|screenSize"
android:launchMode="singleTask"
android:screenOrientation="portrait"/>
P.s: read Launch Mode on this link:
https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

There are couple of options to try -
1) start the new activity with startActivityForResult()
2) in the manifest file, set the android:launchMode="singleTop" of the parent activity
Hope it helps

You might be creating a new instance of your parent when you click the Navigation button. A simple approach is to just finish your activity so whatever activity in the backstack will simply be resumed.
Something like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

Related

Animation on Android Toolbar Back Button

I have an activity wich has a Toolbar which displays a back button.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:title="#string/app_name"
/>
The back button is enabled like this:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_about);
setSupportActionBar(toolbar);
//noinspection ConstantConditions
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
I call this activity from my main activity like this:
Intent intent = new Intent(this, AboutActivity.class);
startActivity(intent);
The Activity's parent is defined in the manifest
<activity android:name=".AboutActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".EntryActivity" />
</activity>
So far everything works fine, except that the transition animation is wrong when using the back button in the Toolbar.
When I open the activity, it slides in from the right.
When I press the phone's physical back button it slides out to the right again. This is correct.
However when using the Toolbar back button it slides out to the left. This looks wrong. How can I change this, so it duplicates the behaviour of the physical back button?
When you press the Actionbar Up button, AppCompatActivity detects this button press in its onMenuItemSelected() call, and invokes onSupportNavigateUp(). This method determines the "parent activity" Intent and uses that to navigate up. Because it's using an Intent to (re-)open the previous activity, it uses the same animation it would for opening a "new" screen.
Assuming you don't care about the particular niceties of the "Up Navigation" pattern (which it sounds like you do not, as comments have led me to believe you don't have lateral navigation and you can't get to your second activity from anywhere other than your first activity), you can side-step all of this built-in "up" behavior by overriding the onSupportNavigateUp() method.
#Override
public boolean onSupportNavigateUp() {
finish();
return true;
}
This will mean that pressing the Actionbar Up button always simply finish()es your activity, so (like I said before) you lose out on all the smart built-in "up" behavior... but you didn't want that anyway.
You could also handle the Actionbar Up button in onOptionsItemSelected(), but I prefer the other way since I think it's a little more obvious that you're hijacking the system's up behavior.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
With either of these in place, you can remove the "parent" definitions from your manifest, since now they're not used.
Try this:
override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}
This is because the default launchMode of activities is standard.
The documentation of standard states the following:
The system always creates a new instance of the activity in the target task and routes the intent to it.
You can solve this by using android:launchMode="singleTop" on the parent/starting activity.
If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.
For more information see Tasks and the back stack and android:launchMode.

Android: I can't seem to get my custom toolbar's back arrow to act like the device's physical back arrow

I have the back arrow implimented and working but instead of just finishing the activityB and going back to the previous view (fragment) the user was on in activityA. It instead closes activityB and reloads activityA which means you have to go back through a few menus to get back to where you were.
The physical back arrow does exactly as I want it to in that it just closes the activtyB and puts the user back to where they were within activtyA originally before they moved to activtyB.
This is my current code for the toolbar back arrow but maybe I'm just not fully grasping how the physical back arrow works. I appreciate any and all advice!
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
For every activity you have to add parent activity in Android manifest file.
<activity
android:name="com.example.myfirstapp.ActivityC"
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>
If you specified MainActivity as parent to ActivityC then it will go to MainActivity. If you specified ActivityB it will move to ActivityB on click on back button in ActionBar.
For more see at here
You need to override the onOptionsItemSelected method of your activity:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
onBackPressed();
} else {
super.onOptionsItemSelected(menuItem);
}
return true;
}

How to return one activity back with actionbarActivity back button?

I made a app with multiple activities. And there are some problem that I cannot solve on my own.
For example, I have 4 activities named "MainMenu","Secondpage","Thirdpage", and a "Fourthpage". From The MainMenu , I pass a id with putExtras() to pass over to the Secondpage so that the Secondpage gets the id and sets it into a url to get the json data from the web. After the "Secondpage" gets the json data,it will display it in a listView. The user will now be able to click the listView to mover over to the "Thirdpage" that contains the id and the data passed from the "Secondpage". Now the problem is that from this "Thirdpage", I want to go back to the "Secondpage" but I cannot find a way to do this.
I've set the manifest file like the follwing, however, I'm not sure if I'm doing it right. Or should I set something up in the ActionbarActivity class?
Sample
<activity
android:name=".Secondpage"
android:label="#string/title_activity_first_time_user"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Thirdpage"
android:label="#string/title"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.me.Secondpage" />
When you say
I want to go back to the "Secondpage"
you mean like if you have pressed the back button?
If so, you can just call finish(); in your third activity to do that.
EDIT:
In your third Activity, add
getActionBar().setDisplayHomeAsUpEnabled(true);
or, if you are using the support library
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
in your onCreate(Bundle b) method
And
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
elsewhere in your class.
Just call
onBackPressed()
if you want to have a behaviour like the user uses his back button. You should return to the SecondPage without any problems.
EDIT:
If you want to use the back button in the action bar check this one out.

Must I specify the parent activity name in the Android Manifest?

I have an activity that starts another activity.
Is it mandatory that I specify the parent activity in the Android Manifest?
Im asking this because there might be other activities that will start this one as well, so should I specify all of them?
android:parentActivityName="com.example.myfirstapp.MainActivity"
As per docs -> section android:parentActivityName:
The system reads this attribute to determine which activity should be started when the user presses the Up button in the action bar. The system can also use this information to synthesize a back stack of activities with TaskStackBuilder.
So you only need to specify that if you're going to use up-navigation (as opposed to navigation by back button) or TaskStackBuilder. In other cases, you don't need it.
For more information, see the up-navigation docs. (Archived from the original, which now redirects to the Jetpack Navigation docs on designing navigation graphs)
While it should be defined if upward navigation or backstack synthesis is desired, note that the attribute android:parentActivityName was introduced in API Level 16.
For prior releases, parent activity information is accessed from attributes defined inside of a <meta-data> tag that is declared inside of the child <activity> tag.
Example:
<activity
android:name=".DetailActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
Inside of the <meta-data> tag, set the android:name attribute to android.support.PARENT_ACTIVITY, and the android:value attribute to the parent activity class name (i.e. the same class name as that assigned to android:parentActivityName).
Unless API level is known, both the <meta-data> and inline specifications are recommended.
For more on specifying the parent activity, see: https://developer.android.com/training/implementing-navigation/ancestral.html#SpecifyParent
In addition, consider defining the android:launchMode attribute inside of your main <activity> tag to set the desired behavior of upward navigation: https://developer.android.com/guide/topics/manifest/activity-element.html#lmode
You don't necessarily need to define the parentActivity in the AndroidManifest.xml. You can use the below code for the back navigation enabled:
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
And implement this:
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
But if you define the parentActivity in the manifest, then the system reads this attribute to determine which activity should be started when the user presses the Up button in the action bar. i.e, it will create a new instance of the parentAcivity, means it will call the onCreate() of the parent activity.
You have to specify every Activity in the manifest which you call via Intent or Launchers, that the system can find it. So, mark one Activity as the Launcher that your App can get started and register every other Activity, which you call in your App.
If you have a BaseActivity like this:
public class BaseActivity extends Activity{}
public class MyActivity extends BaseActivity{}
than you only have to register MyActivity, because BaseActivity is not called by the system but you.
No its not necessary to specify parent activity in manifest like this
android:parentActivityName="com.example.myfirstapp.MainActivity"
for navigationUp you can also use setDisplayHomeAsUpEnabled(true); and onSupportNavigateUp() method Take a Look at this

How to handle "Up" button?

How to handle "Up" button (SDK version 11+)? I am referring to the one at the top of screen, that holds the application icon.
In Android Design articles it was named as "Up button", but I didn't found it (or similar) in KeyEvent fields.
Implement onOptionsItemSelected() and watch for android.R.id.home "menu" events, as is described in the documentation.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// do something useful
return(true);
}
return(super.onOptionsItemSelected(item));
}
First change your AndroidManifest.xml file to have a parent activity declared. Eg
<activity android:name=".theory"
android:parentActivityName=".MainActivity"
android:label="#string/theory"
/>
<activity android:name=".experimental"
android:parentActivityName=".MainActivity"
android:label="#string/exp"
/>
Do this for all the activities other than the MainActivity.
Note the parentActivityName xml code
Then go to the respective java files and add the following code
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
You have you up button enabled now.

Categories

Resources