Start new intent then open a specific fragment - android

I want to start an intent then automatically selecting a fragment after clicking a button.
This is the function of my button.
startActivity(new Intent(AlertNotif.this, HomeActivity.class));
In my HomeActivity.class, it has a BottomNav for different fragments.
bottom_nav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_nav);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);
switch (item.getItemId()){
case R.id.nav_map:
setFragment(mapFragment);
return true;
case R.id.nav_cctv:
setFragment(cctvFragment);
return true;
My default fragment for the HomeActivity is the mapFragment, i want to set it to cctvFragment after clicking the button, instead of the default fragment. I also tried changing the HomeActivity.class to CctvFragment.class, but it is still opening to default.

On the onCreate of the activity you are calling HomeActivity use:
navigation.selectedItemId = R.id.your_id
Where navigation is your layout in the xml for the bottomNavigationView and your id is the id of your navigation item!
All it does is call your onNavigationItemSelectListener when the activity is created.
Remember to set the onNavigationItemSelectListener before you use this code
This code is in Kotlin, I hope is not a problem

Related

Start an activity from fragment using bottom navigation bar

In my app I have 5 fragments. In order to switch between them i use bottom navigation bar.
Also, i need to open activities from my fragments, but when I open new activity it opens in each of the fragments. (I mean over the fragments, in each case)
How to make the activity open in one case?
My code:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
fragmentManager.beginTransaction().hide(active).show(fragment1).commit();
active = fragment1;
return true;
case R.id.nav_bookmark:
fragmentManager.beginTransaction().hide(active).show(fragment2).commit();
active = fragment2;
return true;
case R.id.nav_blog:
fragmentManager.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
return true;
case R.id.nav_notification:
fragmentManager.beginTransaction().hide(active).show(fragment4).commit();
active = fragment4;
return true;
case R.id.nav_account:
fragmentManager.beginTransaction().hide(active).show(fragment5).commit();
active = fragment5;
return true;
}
return false;
}
};
Thanks!
This is clearly a FragmentManager issue - and not an Activity issue.
Instead .hide().show() use .replace().
For reference, see the documentation.
According to the Android documentation, an activity is always placed on top of the current activity stack.
So if you have the MainActivity with the BottomNavigationView and the fragments, starting a new Activity will open the activity above the MainActivity with the BottomNavigationView. So it is not possible to open an Activity only in one of the BottomNavigationView Fragments.
To achieve the desired behaviour, consider using another Fragment instead.

Back Button in ActionBar of App

Generally switching from one activity to another activity hamburger icon is replaced with back arrow. I want to control the functionality of that arrow. I have seen many contents here but most of them were related to hardware's back button. How can I control that?
I am trying the functionality in case of fragments. Also I have Navigation drawer attached with the hamburger icon.
I tried this-
if(id == android.R.id.home){
getSupportFragmentManager().beginTransaction().replace(R.id.main_container, new AmbulanceMap()).commit();
getSupportActionBar().setTitle("Book A Ride");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
but doesnt work as I hoped.
I want my back button to change the fragmentto previous fragment.
I had the same problem once. Just like you, things like checking if Android.R.id.home is clicked didn't work.
But I solved it using that:
Set navigation listener to toolbar:
toolbar.setToolbarNavigationClickListener(v -> onBackPressed());
If it should be within fragment:
Create an public method in activity.
In fragment's onAttach (or later) cast getActivity() to your activity and call method you was defined previously.
Example:
// YourActivity
public void setHomeListener(OnLickListener listener){
toolbar.setToolbarNavigationClickListener(listener);
}
//Fragment's onCreate
((YourActivity)getActivity()).setHomeListener(v -> onBackPressed());
//Fragment's onDestroy
((YourActivity)getActivity()).setHomeListener(null);
And, of course, set home us up enabled to show back arrow.
EDIT
if you don't use labmdas u should use:
(YourActivity)getActivity()).setHomeListener(new OnClickListener() {
#Override
public void onClick(View v) {
YourFragment.this.onBackPressed();
}
});
The back button of the ActionBar is a menuItem so you need to override onOptionsItemSelected like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
//Your back button logic here
break;
}
return true;
}
Also donĀ“t forget to add getSupportActionBar().setDisplayHomeAsUpEnabled(true); after setting the Toolbar

Does not update Firebase data when activity returns - Bottom Navigation View

I am using Bottom Navigation View and in one of the options we display Firebase data, with a CardView.
But when I get out of this activity, and come back, either with the device back button or the activity arrow, it does not work. Does not display anything.
What could be wrong?
Prints:
Before:
After:
Code Bottom Navigation View:
BottomNavigationView bottomNavigationView =(BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(0);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.ic_home:
break;
case R.id.ic_explore:
Intent intentExplore = new Intent(HomeActivity.this, ExploreActivity.class);
intentExplore.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentExplore);
break;
case R.id.ic_calendar:
Intent intentAgenda = new Intent(HomeActivity.this, AgendamentoActivity.class);
intentAgenda.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentAgenda);
break;
case R.id.ic_person:
Intent intentuser = new Intent(HomeActivity.this, OpcoesUsuarioActivity.class);
intentuser.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentuser);
break;
}
return false;
}
});
Note: In tests in activities without Bottom Navigation or with SlidingTabs, it works normally.
well you are using a wrong approach about the BottomNavigationView you need to use Fragment not Activities check this. About your problem when you are clicking on any item you puts your activity onPause that mean the activity still at the stack but is not visible, when clicking back you just finish the current activity and back to the last one with calling the onResume method the onCreate is not called, so put your RecyclerView work at your onResume method, or call finish() when leaving the activity.

Android - Navigation Up from Activity to Fragment

I'm developing some application and I have one problem.
I have :
1. Activity A (Navigation Drawer pattern) with ListFragment in FrameLayout:
xml:
<FrameLayout
...>
</FrameLayout>
<LinearLayout
...>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Activity B which shows the detail data of ListView in ListFragment.
How can I go back (using Navigation Up Button) from activity B to Activity A with saving UI of the ListFragment (Activity re-creates if I go back using Home Back).
Btw, if I press the back button on my phone, activity does not re-create and returns in previous state.
When you use UP navigation, then the previous activity is recreated. To prevent that from happening while you preserve the UP navigation, you can get the intent of the parent activity, and bring it to front if it exists, otherwise create it if not.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent parentIntent = NavUtils.getParentActivityIntent(this);
parentIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(parentIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I also specified launchMode="singleTop" in the Manifest. but I am not sure if that was necessary.
One thing you can do to prevent the first activity to recreate is by just calling finish() on the second activity when that back button is pressed.
Not tested, but I believe the id is android.R.id.home, so all you have to do is override onOptionsItemSelected in the second activity, like this:
/**
* Handles the selection of a MenuItem.
*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Action bar back button from un activity to previous activity fragment

I'm using a navigation drawer in a main activity with many fragments
For example :
I have fragment 1 for presentation
And fragment2 that contains a listView.
If I click an item in the listView it will launch another activity
When i click the action bar back button i want to go to the main activity fragment2
But the fragment1 is used
How can I use the second fragment
Thanks for your answers
In your second activity's onCreate, put(although i think you already have) :
getActionBar().setDisplayHomeAsUpEnabled(true);
and inside second activity's options handling(i think you've done this too) :
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
//perhaps use intent if needed but i'm sure there's a specific intent action for up you can use to handle
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and inside your first activity's onCreate's intent handler for example :
if(Intent.ACTION_VIEW.equals(action))
you set the current page of the viewpager with viewpager.setCurrentItem(position). If you want to change the page for different items you could use intent.putExtra() in your second activity and then retrieve it in your first activity.
I have added the code below in my second activity :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
onBackPressed();
return true;
}
It's working as I expected
is it a clean solution ?
Thanks

Categories

Resources