The almost-solution is How to launch activity and show specific fragment but I unable to connect the dots on how to solve it.
The MainActivity.java
have navigation drawer with few fragments. In StudentFragment
(since no logged in user, only view login button), I launched a login activity via a button. The login is fine and view user profile, but when I press back to go back to StudentFragment
, the layout won't update. It should view a layout for logged in user. In order to view it, I have to close and open the app again.
To tackle this problem, I was thinking on how to relaunch MainActivity
with viewing StudentFragment
by clear the top stack.
The second answer in link can be use as solution but I need more explanation of it :(
My codes:
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// view layout home here
Fragment fragment = new HomeFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
}
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
Fragment fragment = null;
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the home action
fragment = new HomeFragment();
} else if (id == R.id.nav_outline) {
fragment = new OutlineFragment();
getSupportActionBar().setTitle("Course Outline");
} else if (id == R.id.nav_PDPA) {
fragment = new PDPAFragment();
getSupportActionBar().setTitle("PDPA 2010");
} else if (id == R.id.nav_copyright) {
fragment = new CopyrightFragment();
getSupportActionBar().setTitle("Copyright Act 2012");
} else if (id == R.id.nav_laws) {
fragment = new LawsFragment();
getSupportActionBar().setTitle("List of Laws");
} else if (id == R.id.nav_students) {
fragment = new StudentsFragment();
getSupportActionBar().setTitle("Student");
} else if (id == R.id.nav_lecturers) {
fragment = new LecturerFragment();
getSupportActionBar().setTitle("Lecturer");
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
EDIT by following Sanjeet answer
In LoginActivity
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
setResult(Config.RESULT_CODE, intent);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
In MainActivity
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Config.RESULT_CODE && requestCode == Config.REQUEST_CODE) {
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentById(R.id.nav_students);
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
}
But the fragment shown after relaunch MainActivity
is HomeFragment
, not StudentFragment
.
Start the Login Activity using
startActivityForResult(intent, REQUEST_CODE);
Now, after successful login set the result and finish the Login activity after setting the result code.
Intent intent = new Intent();
setResult(RESULT_CODE, intent);
finish();
Now in your MainActivity's class onActivityResult() based on REQUEST_CODE and RESULT_CODE restart your Student Fragment.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_CODE && requestCode == REQUEST_CODE) {
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.