Android ActivityGroup Back Button - android

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

Related

How controller a VideoView in Fragment when I pressed onBackpressed in parent Activity?

I have an Activity (A) that contains a ViewPager which calls a Fragment to present information to the user (this Activity A is called from an Activity B). My Fragment contains a VideoView which plays an MP4 with GIF behavior. My problem is when I want to return to Activity B, since when doing a Finish () in the onBackpressed in Activity A, for a few milliseconds the videoView looks over the contents of Activity B.
UPDATE
I discovered that the problem is due to the ViewPage.
It happens that when I'm in position 0, calling the onBackPressed method works everything ok. But when the onBackPressed method is called from another position, closing the Activity momentarily shows the VideoView of the previous position.
#Override
public void onBackPressed() {
if (mViewPager.getCurrentItem() != 0) {
//I don't know what to do here
}
super.onBackPressed();
}
Please finish your call when you Back pressed.
public void onBackPressed() {
if (/--logic--/) {
finish();
}
}

Navigating back to previous Activity

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();
}
});

back stack keeps adding when using Intents

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)
}

Home button parent fragment-child activity

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.

Navigate up from activity back to fragment

Let's say I have two activities called A_Activity and B_Activity. A_Activity has 3 fragments named A_Fragment, B_Fragment, and C_Fragment. A_Activity acts as a base activity for the three fragments.
From B_Fragment, it is possible to navigate to B_Activity and from B_Activityit is possible to navigate up to B_Fragment. from A_Activity it is possible to navigate to A_Fragment, B_Fragment, orC_Fragment. Lastly, from A_Fragment, B_Fragment, or C_Fragment, it is possible to navigate to any other fragment.
Here's an arbitrary representation of the hierarchy of activities and their respective fragments:
A_Activity
A_Fragment
B_Fragment
B_Activity
C_Fragment
Let it also be a given that A_Fragment is the default fragment for A_Activity.
My current solution to this is: in B_Activity's home icon selected, I am creating a new intent:
Intent intent = new Intent(B_Activity.this, A_Activity.class);
intent.putExtra("data_to_start_B_Fragment", "B"); // instead of starting A Fragment
startActivity(intent);
What is the proper way to navigate to B_Fragment from B_Activity?
From B_Fragment, starting B_Activity using startActivityForResult(...) by getApplicationcontext not getActivity().getApplicationContext().
In Activity B, you use setResult(data here) to return fragment and finish ActivityB to back fragment B.
From B_Activity to B_Fragment (if you start B_Activity from B_Fragment), you just need to detect action from either home button or system back button and then kill the Activity by calling
finish()
e.g. in B_Activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (id == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}

Categories

Resources