Back Arrow in Activity - android

I have 3 tabs with lists of different video.In first tab when I click the item of my lyst the new activity started and play the video,this activity have parent my main activity with lists and this mainactivity have 1tab as main tab.But when I click in item in 2 tab,thid videoplayer started too,but back arrow go to main activity and main activity have 1 tab main,not second.It's very hard,but Im trying to explain:-)
How can I change this behavior,to go to 2 tab when click back arrow?

If when you have a toolbar then just add the following code in the onCreate. What this will do, it will work as the Back key of the smartphone. so it will close the current activity, and bring you back to what ever was on.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { finish(); } });
}

Related

Android - Remove back button from action bar for a particular fragment

I have an app (Java) which loads a fragment (Fragment A). This fragment has a recyclerView which in turn loads a second fragment (Fragment B). I want Fragment B to have a back arrow that can return to Fragment A, but I don't want the back arrow in Fragment A.
I added this code to the Main Activity:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
But this causes the back arrow to appear in every fragment.
How can I have the back arrow appear only on second level fragments (like B)?
you can hide the button on the wanted fragment like this
#Override
public void onResume() {
super.onResume();
((AppCompatActivity)getActivity()).getSupportActionBar(). setDisplayHomeAsUpEnabled(false);
}
#Override
public void onStop() {
super.onStop();
((AppCompatActivity)getActivity()).getSupportActionBar(). setDisplayHomeAsUpEnabled(true);
}

How to set back button on toolbar to do the same thing as system back button?

In my main activity i have ViewPager and on my second page i have CardView, when i click on CardView, it opens new activity with some info. Problem is when i want to go back, if i click back button in toolbar it goes back to main activity but ViewPager is set on first page (not the second one where CardView was). However, when i click back on my system button it goes correctly to main activity on second page.
This is how i open new activity when i click on CardView
adapter.setListener(new CaptionedImagesAdapter.Listener(){
public void onClick(int pos){
Intent intent = new Intent(getActivity(),
PizzaDetailActivity.class);
intent.putExtra(PizzaDetailActivity.EXTRA_PIZZA_ID, pos);
getActivity().startActivity(intent);
}
});
Setting Up (back) button in toolbar
Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(tb);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
And i placed MainActivity as parent in Manifest
<activity
android:name=".OrderActivity"
android:label="#string/label_porudzbina"
android:parentActivityName=".MainActivity" />

How to get Up Navigation behavior exactly like back button pressed

How can I get the Up Navigation button to behave like the back button? When I press the back button all my previous activities remain as the were prior.
When I press the up button my activities return to initial state. I tried hooking up onBackPressed(); to case R.id.home on my onOptionsItemSelected but it still behaves differently.
Visual:
Back Button Behavior-
https://www.flickr.com/photos/151974643#N07/35702817566/in/dateposted-public/ Up Button Behavior - https://www.flickr.com/photos/151974643#N07/35702812926/in/dateposted-public/
Any help would be appreciated. Thank you!
You can do it several ways, put this code in onCreate() method of your activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YOUR_ACTIVITY.this.onBackPressed();
}
});
Hope it helps.

Android Toolbar back enable and set subtitle

I'm using android.support.v7.widget.Toolbar and AppCompatActivity. I've enabled back up button like this supportActionBar.setDisplayHomeAsUpEnabled(true);.
The fragment in side the activity will set the title and subtitle in onResume() like this
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setTitle(title);
activity.getSupportActionBar().setSubtitle("Bingo");
The problem is, when the fragment shows up onResume is called but subtitle is not shown. When I press power OFF and ON, means fragment goes to pause and resumes again. Now, subtitle is visible. I've tested on other android phone as well.
Can you please help me finding out the problem?
This is because the toolbar is not rendered when you are setting the subtitle.
Try this code,Set title and subtitle inside this method
private void setupToolbar(){
toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
if(toolbar != null){
setSupportActionBar(toolbar);
}
toolbar.post(new Runnable()
{
#Override
public void run()
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(mTitle);
getSupportActionBar().setSubtitle("Subtitle);
}
});
}

How to intercept the fragment call to popBackStack() in MainActivity?

In my Application, i'm transitioning to a DetailsFragment when the user clicks on a list item. and there are TWO options to be back at the main Fragment (the List Fragment).
Press the back button. (no problem here, because I handle this in onBackPressed() in MainActivity)
Press the Toolbar back arrow (Here is my problem).
When the user presses the toolbar back arrow, I call the following
getActivity().getSupportFragmentManager().popBackStack();
how can I intercept this event in MainActivity?
(There are some manipulations I am doing in MainActivity when the List Fragment is visible to the user.
Just put this code
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});

Categories

Resources