I am having Activity A and Two Fragment called Fragment A and Fragment B.
In Fragment B List View is implemented. On Clicking of any list item the new activity get instantiated (Activity B). The Problem with the scenario 2
if the user pres the home button and again resume the activity then activity 2 is getting resumed.After resuming activity if the user press the back button then activity is getting into pause stage instead of returning back into to the parent fragment(Fragment 2).
Manifest for Activity 2
<activity
android:name=".activity.Activity2"
android:label="#string/label1"
android:parentActivityName=".activity.Activity1"
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.test.activity.Activity1" />
</activity>
Let me know what's causing this behavior. How can i retain fragment B when Activity B move into pause state.? or any other solution?
You could override the onBackPressed of your second activity, so that you always return to the first activity with fragment B shown.
#Override
public void onBackPressed() {
Intent intent = new Intent(this, Activity1.class);
intent.putExtra("some tag", "some text");
startActivity(intent);
}
And in your first activity do something lke that:
#Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
if(intent.getStringExtra("some tag").equals("some text"))
{
methodToDisplayFragmentB();
}
}
Hope this helps.
Related
I have two Activities A and B.
Activity A has a Tablayout with some Tabs. When I navigate from A to B I use this:
Intent intent = new Intent(A, B.class);
A.startActivity(intent);
When I now navigate back from B to A I have a question:
1) When using Android's back button, the selected tab / scrolling position from A was remembered
2) When using an Intent or NavUtils.navigateUpFromSameTask(this); then the selected tab and scroll Position is NOT remembered but set to initial value
Can someone explain me what is going on here?
1) when navigation from activity A to B, the android system does not destroy activity A, but takes it to the back stack and adds B to the foreground. thats why when you press the back button or call onBackPressed() from the java code activity B is destroyed and A is set to the foreground. here is an example from the docs : Understand Tasks and Back stack
2) when using an intent/navigateUpFromSameTask activity A is recreated and set to the foreground and B is set to the background, it's like adding another activity A to the stack so it will be A,B,A but if you press the back btn then you will be back to B and then A.
if you want to keep the scroll position and other data in activity A you call the onBackPressed in B or use the onSaveInstanceState to save the data and use it in the onCreate .
here is an example of saved instance:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("VariableName", variableData);
savedInstanceState.putString("VariableName", variableData);
savedInstanceState.putString("VariableName", variableData);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.penguin_main);
if(savedInstanceState!=null){
bookData = (String) savedInstanceState.getSerializable("VariableName");
bookData = (String) savedInstanceState.getSerializable("VariableName");
bookData = (String) savedInstanceState.getSerializable("VariableName");
}
}
You can set the current scroll position and tab position in activity A's on overriding onSaveInstance(Bundle savedInstanceState) method. When return to activity you can get onRestoreInstanceState(Bundle savedInstanceState) to restore it.
Hope it helps :)
Because NavUtils.navigateUpFromSameTask() just calls startActivity() and if android:launchMode="standard" the activity will be instantiated and created again and that is why can not remember the previous selected tab. To solve this issue you can override onNavigateUp() and inside that setCurrentItem(index) the index of tab you want to be displayed.
#Override
public boolean onNavigateUp() {
myViewPager.setCurrentItem(position, true);
return true;
}
Edit
You can use another solution to solve the problem by setting android:launchMode="singleTop" on activity but this solution may not applicable in all the application.
When you start an Activity, the first page will be opened! But when the back button is pressed, it navigates between the saved state of the activityTo simulate the back button pressed, you can try this:
#Override
public void onBackPressed() {
finish(); //your choice, thought not needed as super.onBackPressed(); is called if nothing is assigned here
}
or on toolbar back button clicked click:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
I have an activity A which uses a fragment A which has a list. The activity A can call on a search activity. The problem that I am seeing is if I were to go the search activity and then go back to the activity A, from there fragment A gets loaded. If I select an item from Fragment A i get taken to Fragment B, if i were to press the back button i have to click it 2 or 3 times. Any ideas? Do i need to start the search activity with a parameter so it does not add to the back stack. I have tried the Flag to Intent.FLAG_ACTIVITY_CLEAR_TOP when starting the search activity but the issue occurs.
Java:
Activity A:
public void popFragment(){
if(getSupportFragmentManager().getBackStackEntryCount() > 1) {
getSupportFragmentManager().popBackStack();
getSupportFragmentManager().executePendingTransactions();
}
}
Fragment A:
private void showSearchActivity(){
Intent intent = new Intent(context, SearchActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, 0)
}
I have six Tabs in MainActivity, the second tab have a listview, when user press on the listview item, its open a new Activity with Action bar, so, when user press on the back button of the second activity, I want to go to previous tab (second tab) of Main Activity, but its loading the First Tab (Home Tab).
How I can resolve this problem?
Using the Up navigation to return to the parent activity recreates the parent activity. When the parent activity is recreated you lose your selected tab. Rather than saving the selected tab, it is easier to just not recreate the parent activity. This can be done by adding the following line to the parent activity section of your AndroidManifest file.
android:launchMode="singleTop"
This will prevent the parent from being recreated and thus your previously selected tab will still be in the same state.
For example, if the MainActivity is the parent with the tabs, then the manifest would look something like this:
<activity android:name=".MainActivity"
android:launchMode="singleTop">
...
</activity>
See also:
ActionBar up navigation recreates parent activity instead of onResume
How can I return to a parent activity correctly?
We have three cases here. The actual back button (regardless it is hardware or software), the Action Bar's parent ("Up") button, and both buttons:
Back button case:
When you call the SecondActivity, use startActivityForResult() to keep MainActivity informed of the SecondActivity's lifecycle. When "back" is pressed, capture this event in MainActivity.onActivityResult() and switch to the second tab:
MainActivity: where you currently start your activity:
// Start SecondActivity that way. REQUEST_CODE_SECONDACTIVITY is a code you define to identify your request
startActivityForResult(new Intent(this, SecondActivity.class), REQUEST_CODE_SECONDACTIVITY);
MainActivity: onActivityResult():
// And this is how you handle its result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == REQUEST_CODE_SECONDACTIVITY && resultCode == RESULT_CANCEL) {
switchToTab(2); // switch to tab2
}
// else... other cases
}
Action Bar's "Up" button case:
If this behaviour has to be connected to the Action Bar's "Up" button instead of the back button, you have to override getSupportParentActivityIntent() or getParentActivityIntent() depending on whether you are using the support library or not.
SecondActivity: get[Support]ParentActivityIntent():
#Override
public Intent getSupportParentActivityIntent() { // getParentActivityIntent() if you are not using the Support Library
final Bundle bundle = new Bundle();
final Intent intent = new Intent(this, MainActivity.class);
bundle.putString(SWITCH_TAB, TAB_SECOND); // Both constants are defined in your code
intent.putExtras(bundle);
return intent;
}
And then, you can handle this in MainActivity.onCreate().
MainActivity: onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
...
final Intent intent = getIntent();
if (intent.hasExtra(SWITCH_TAB)) {
final int tab = intent.getExtras().getInt(SWITCH_TAB);
switchToTab(tab); // switch to tab2 in this example
}
...
Both buttons case:
Should you wish to handle both buttons the same way (regardless this is a good idea or not, I just don't know), both solutions above can be implemented concurrently with no problem.
Side note: to determine whether this is a good idea or not, this official guide may help. Especially the section "Navigating Up with the App Icon".
I have an Activity named A,
in this, a ListView and one button are there.After clicking on this button,List View is shown and from this list view, by clicking on a its items, I can move to Activity B.
Now the problem is this when I come back from Activity B -> Activity A then, I see the Button only not the list view.
because I am calling intent of Activity A..
Code
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent i;
i = new Intent(B.this, A.class);
startActivity(i);
finish();
super.onBackPressed();
}
In Activity B, I have the above implementation and i am using finish() in On Pause() condition also.
I want to see the List View with buttons.
Do i need to call whole code again to show the ListView or is there any other way to resolve this problem??
Is there any way to save the previous activity view?
Its because you are finishing Activity A before switching to Activity B. If you want your ListView as it is then do not finish Activity A and then try. If still your data does not visible then bind your listview again in onResume() of Activity A.
//removed finish().
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent i = new Intent(B.this, A.class);
startActivity(i);
super.onBackPressed();
}
You are creating your activity aagain since u hv called the finish
method and when u go back it calls onCreate rather it should call
onResume method of lifecycle.
Just do one thing.rmove finish() from your code
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent i;
i = new Intent(B.this, A.class);
startActivity(i);
super.onBackPressed();
}
onResume called when activity start or when you return from another activity as per life cycle of Activity, its better to keep your code inside onResume, which will refill your listview with data.
SO when you called activity B from A (in Activity A fill your listview inside onResume method) and when you press back, its again callon onResume and fill your listview.
Instead# doing code inside onBackPressed of activity B.
I have a small issue. I have looked all over the internet but I cannot find a solution to my problem. The problem I have is:
I have a TabHost that has 3 tabs. The first tab opens Activity A. In Activity A, I can press in a listview and it will change the setContent() to Activity B. When I press the back button in Activity B, the onBackPressed() function of Activity A gets called.
How can I close Activity B and go back to Activity A onBackPressed()?
This is how i did it
private void onBackPressed(){
RootActivity parentActivity;
parentActivity = (RootActivity) this.getParent();
parentActivity.switchToSecondActivity();
} // here RootActivity is the tabhost
in RootActivity
public void switchToSecondActivity(){
tabHost.setCurrentTab(SECOND);
} //SECOND is an integer pointing location of the second activity. it starts from 0