Full-screen fragment to fragment transition displayed main activity during fragment transition - android

In my android application, I had one main activity(i.e.Home) and two full-screen fragments. I can access theses three using Navigation Drawer. These two fragments act as the separate screen but actually its top on main activity.
In my case, I clicked the first fragment then I clicked the second fragment. During this time, I closed the first fragment and displayed the second fragment. But, main activity screen was now displayed one second during fragment transition.
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_out);
if(!item.isChecked())
getSupportFragmentManager().popBackStackImmediate(null, POP_BACK_STACK_INCLUSIVE);
item.setChecked(true);
if (previousMenuItem != item.getItemId()) {
if (item.getItemId() == R.id.fragment_two) {
initialiseFragmentTwo();
} else if (item.getItemId() == R.id.nav_main_activity) {
getSupportFragmentManager(). popBackStackImmediate(null, POP_BACK_STACK_INCLUSIVE);
getSupportActionBar().setTitle(R.string.app_name);
}
else if(item.getItemId() == R.id.nav_fragment_one){
initialiseFragmentOne();
}
}
previousMenuItem = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer != null) {
drawer.closeDrawer(GravityCompat.START);
}
return true;
}

Instead of using POP_BACK_STACK_INCLUSIVE try to use replace
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
Class fragmentClass = null;
if (id == R.id.nav_camera) {
fragmentClass = FragmentOne.class;
} else if (id == R.id.nav_gallery) {
fragmentClass = FragmentTwo.class;
} else if (id == R.id.nav_slideshow) {
fragmentClass = FragmentOne.class;
} else if (id == R.id.nav_manage) {
fragmentClass = FragmentTwo.class;
} else if (id == R.id.nav_share) {
fragmentClass = FragmentOne.class;
} else if (id == R.id.nav_send) {
fragmentClass = FragmentTwo.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
See here below links
http://chrisrisner.com/Using-Fragments-with-the-Navigation-Drawer-Activity
https://www.simplifiedcoding.net/android-navigation-drawer-example-using-fragments/

Related

Change Fragment Layout (RTL , LTR) on runtime when changing language

i am using a Drawer Navigation activity to handle fragments , and i have an options menu to change Locale like this :
public class DrawerNavigationActivity extends AppCompatActivity implements NavigationView
.OnNavigationItemSelectedListener, AgendaMainFragment.OnFragmentInteractionListener,
MediaMainFragment.OnFragmentInteractionListener, VideosFragment
.OnFragmentInteractionListener, PhotosFragment.OnFragmentInteractionListener,
ThemesMainFragment.OnFragmentInteractionListener, AdresseMainFragment
.OnFragmentInteractionListener, InfoMainFragment.OnFragmentInteractionListener,
NewsMainFragment.OnFragmentInteractionListener, BlockMainFragment
.OnFragmentInteractionListener, DemosMainFragment.OnFragmentInteractionListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_navigation);
setTitle(R.string.app_name);
//########################################################### set initial fragment
Bundle b = getIntent().getExtras();
Fragment fragment = null;
if(b.getInt("link") == 1) {
fragment = new AgendaMainFragment();
}else if(b.getInt("link") == 2){
fragment = new MediaMainFragment();
}else if(b.getInt("link") == 3){
fragment = new ThemesMainFragment();
}else if(b.getInt("link") == 4){
fragment = new AdresseMainFragment();
}else if(b.getInt("link") == 5){
fragment = new InfoMainFragment();
}else if(b.getInt("link") == 6){
fragment = new NewsMainFragment();
}else if(b.getInt("link") == 7){
fragment = new BlockMainFragment();
}else if(b.getInt("link") == 8){
fragment = new DemosMainFragment();
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frag_drawer_container, fragment);
ft.commit();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.drawer_navigation, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
switch (id) {
case R.id.fr:
LocaleHelper.setLocale(getBaseContext(), "fr");
LocaleHelper.updateGlobalConfig(getBaseContext());
ft.detach(getVisibleFragment()).attach(getVisibleFragment()).commit();
return true;
case R.id.ar:
LocaleHelper.setLocale(getBaseContext(), "ar");
LocaleHelper.updateGlobalConfig(getBaseContext());
ft.detach(getVisibleFragment()).attach(getVisibleFragment()).commit();
return true;
case R.id.en:
LocaleHelper.setLocale(getBaseContext(), "en");
LocaleHelper.updateGlobalConfig(getBaseContext());
ft.detach(getVisibleFragment()).attach(getVisibleFragment()).commit();
return true;
case R.id.es:
LocaleHelper.setLocale(getBaseContext(), "es");
LocaleHelper.updateGlobalConfig(getBaseContext());
ft.detach(getVisibleFragment()).attach(getVisibleFragment()).commit();
return true;
}
return super.onOptionsItemSelected(item);
}
public Fragment getVisibleFragment(){
FragmentManager fragmentManager = DrawerNavigationActivity.this.getSupportFragmentManager();
List<Fragment> fragments = fragmentManager.getFragments();
if(fragments != null){
for(Fragment fragment : fragments){
if(fragment != null && fragment.isVisible())
return fragment;
}
}
return null;
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment = null;
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
super.onBackPressed();
return true;
} else if(id == R.id.nav_agenda) {
fragment = new AgendaMainFragment();
} else if (id == R.id.nav_gallery) {
fragment = new MediaMainFragment();
} else if (id == R.id.nav_themes) {
fragment = new ThemesMainFragment();
} else if (id == R.id.nav_adresse) {
fragment = new AdresseMainFragment();
}else if (id == R.id.nav_info) {
fragment = new InfoMainFragment();
}else if (id == R.id.nav_news) {
fragment = new NewsMainFragment();
}else if (id == R.id.nav_blocs) {
fragment = new BlockMainFragment();
}else if (id == R.id.nav_demo_animation) {
fragment = new DemosMainFragment();
}else{
return true;
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frag_drawer_container,fragment);
ft.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onAgendaFragmentInteraction(Uri uri) {
}
#Override
public void onMediaFragmentInteraction(Uri uri) {
}
#Override
public void onFragmentInteraction(Uri uri) {
}
#Override
public void onFragmentPhotoInteraction(Uri uri) {
}
for my fragments i change the bar action title like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_news_main, container, false);
LocaleHelper.updateGlobalConfig(getContext());
getActivity().setTitle(getResources().getString(R.string.fgmt_titre_news));
return view;
}
it's working fin except for an RTL language , when i choose and RTL locale it change the title and the content but keeps the layout orientation LTR , so if i close the application and reopen again it changes the oriontation to RTL but keeps it even for LTR fragments:
by orientation i mean the action bar and the drawer navigation.
Fragment with local Fr :
Change Local to AR (RTL)
close the App and reopen it and navigate to RTL fragment
navigate to another LTR fragment (it keeps RTL Layout Orientation)
what am i doing wrong and what is the best and clean way to achieve it ?

Naviation Drawer calling an activity

How to call an activity from the navigation drawer.
thia ia my navigation drawer:
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_Home) {
Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
HomeFragment homeFragment = new HomeFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.RelativeLayout, homeFragment, homeFragment.getTag()).commit();
} else if (id == R.id.nav_Profile) {
Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_manage) {
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_share) {
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_send) {
Toast.makeText(this, "Send", Toast.LENGTH_SHORT).show();}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
I have an activity called ProfileActivity.java but i dont know how to call an activity through navigation drawer...help
Hello try this hope it may help you
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_Home) {
Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
HomeFragment homeFragment = new HomeFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.RelativeLayout, homeFragment, homeFragment.getTag()).commit();
} else if (id == R.id.nav_Profile) {
//Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
startActivity(new Intent(YOUR_ACTIVITY,ProfileActivity.class));
} else if (id == R.id.nav_manage) {
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_share) {
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_send) {
Toast.makeText(this, "Send", Toast.LENGTH_SHORT).show();}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

Avoid click on same items of Navigation drawer concurrently one after other for fragments and for activities

I am using common Navigation drawer for all activities and fragments but when I am clicking on same item of Navigation drawer also it inflating the same screen. What I want is the same item should not be loaded for second time.
This is my code:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (id == R.id.nav_profile) {
Intent i = new Intent(MainFragmentsActivity.this, ProfileActivity.class);
startActivity(i);
drawer.closeDrawer(GravityCompat.START);
}
}
You should not use common navigation drawer for all activities and fragments. Common design pattern is showing different fragments on the main activity for the navigation drawer menu items. And if the user navigates inside any fragment you should open an activity without any navigation drawer but with a back arrow in action bar.
Now if you dont want to show the same fragment then in
onNavigationItemSelected you can write the logic like below -
Fragment fragment = getSupportFragmentManager().findFragmentByTag("TAG");
if (fragment == null)
{
Create and attach new fragment
}else{
//Do not do anything.
}
Simple thing, store clicked view'd id to a variable and compare if its the same,
int id; // use this variable to store id of clicked view.
#Override public boolean onNavigationItemSelected(MenuItem item) {
if(id != item.getItemId) {
id = item.getItemId(); // store clicked id in variable
// Do your clicking stuffs here...
if (id == R.id.nav_profile) {
Intent i = new Intent(MainFragmentsActivity.this, ProfileActivity.class);
startActivity(i);
drawer.closeDrawer(GravityCompat.START);
}
}
}
For fragments:
if (id == R.id.nav_things_to_do) {
if (id == R.id.nav_things_to_do && viewPager.getCurrentItem() != 0) {
Intent main1 = new Intent(getApplicationContext(), MainFragmentsActivity.class);
main1.putExtra("position", 0);
main1.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
main1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
main1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(main1);
overridePendingTransition(0, 0);
drawer.closeDrawer(GravityCompat.START);
}
else {
drawer.closeDrawer(GravityCompat.START);
}
}
if (id == R.id.nav_all_tasks) {
if (id == R.id.nav_all_tasks && viewPager.getCurrentItem() != 1) {
Intent main2 = new Intent(getApplicationContext(), MainFragmentsActivity.class);
main2.putExtra("position", 1);
main2.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
main2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
main2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(main2);
overridePendingTransition(0, 0);
drawer.closeDrawer(GravityCompat.START);
}
else {
drawer.closeDrawer(GravityCompat.START);
}
}
For Activities:
if (id == R.id.nav_images) {
try {
Intent i = new Intent(MainFragmentsActivity.this,
SelectFilesActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
drawer.closeDrawer(GravityCompat.START);
} catch (Exception e) {
e.printStackTrace();
}
}

Other Fragment is viewed above the current fragment changes on rotation with android studio navigation drawer template

Thank you for spending time on my qeustion. In my app I am using android studio navigation drawer template.
But the problem is that, when I rotate the phone, the previous fragment's layout is shown above the current fragment.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Fragment fragment = null;
Class fragmentClass = null;
/* if (savedInstanceState == null){
try{
fragment = (Fragment) fragmentClass.newInstance();
}catch (Exception e){
e.printStackTrace();
}
}*/
fragment = new Login();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment, fragment);
fragmentTransaction.commit();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment =null ;
Class fragmentClas = null;
if (id == R.id.nav_camera) {
// Handle the camera action
fragment = new Attendance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_gallery) {
fragment = new AddStudent();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
/* FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();*/
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
When you rotate the device, the activity saves its current state and keeps the current fragment then it's destroyed and recreated i.e. onCreate() is called again. You had one fragment before it's destroyed and now it added another in the new call to onCreate() that's why you have 2 fragments. To fix this you should check first if the savedInstanceState parameter in onCreate() is null i.e. it's the first creation of activity not just being restarted due to rotation change or anything, then decide to add the fragment or not instead of adding to whenever the activity calls onCreate()
if(savedInstanceState == null) {
// Add the fragment because it's normal creation
fragment = new Login();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment, fragment);
fragmentTransaction.commit();
}

How to use the Navigation Drawer to switch Activities[Android Studio]

I created an Navigation Drawer Activity in Android Studio 1.4. My Main Activity is already setted up and now I want to switch to another Activity via the Navigation Drawer. The second Activity (AddDataActivity) should collect some User Unputs to create string which should be displayed in my Main Activities ListView.
My problem is, that I dont know how to open the AddDataActivity without "loosing" my navigation Drawer.
Intent AddData = new Intent(MainActivity.this, AddDataActivity.class);
MainActivity.this.startActivity(AddData);
Do I have to copy the whole Drawer Code for each Activity? Or would it be better to use Fragments?
I use Fragments now I avoid this Situation.
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fragment = new ListViewFragment();
int id = item.getItemId();
if (id == R.id.nav_listview) {
fragment= new ListViewFragment();
} else if (id == R.id.nav_add_data) {
fragment= new AddDataFragment();
} else if (id == R.id.nav_settings) {
fragment= new SettingsFragment();
} else if (id == R.id.nav_rooms) {
fragment= new SavedRoomsFragment();
} else if (id == R.id.nav_legal_information) {
fragment = new LegalInformationFragment();
}
ft.replace(R.id.container, fragment);
ft.addToBackStack(null);
ft.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

Categories

Resources