i want call simple Android Activity On click of Slider Menu Item, on place of fragment.
i tried to called activity using fragment but it shows nothing
please help me.
can anyone suggest alternate way rather than this ?
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
Log.e("", "In Fragment "+position);
fragment = new About_us_Activity();
//startActivity(new Intent(this,Home_Activity.class));
break;
case 1:
//fragment = new About_us_frag();
Log.e("", "In Fragment "+position);
break;
case 2:
Log.e("", "In Fragment "+position);
fragment = new About_us_frag1();
break;
default:
break;
}
if (fragment != null) {
Log.e("", "In Fragment");
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
First of all apply onItemClickListener to your drawer list.
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mDrawerLayout.closeDrawers();
displayView(position);
}
});
In your DisplayView method:
private void displayView(int position) {
switch (position) {
case 1:
Intent intent = new Intent(getApplicationContext(), MyActivity1.class);
startActivity(intent);
break;
case 2:
Intent intent1 = new Intent(getApplicationContext(),MyActivity2.class);
startActivity(intent1);
break;
default:break;
}
}
MyActivity1 and MyActivity2 are activities. you can try this method , it worked for me.
Related
I have a Navigation Drawer which has 5 items.
If user selects an Item, I'm loading respective Fragment in FrameLayout
Now, if user pulls out the Navigation Drawer and selects the same Item as previous, I should not load the same Fragment again, so I'm saving the selected position and if previous selected position equals current selection I'm just closing the Navigation Drawer as follows:
String title = null;
invalidateOptionsMenu();
android.support.v4.app.Fragment fragment = null;
if (position == getPrevSelectedItem()) {
// Already selected fragment is being displayed
} else {
switch (position) {
case Numerics.ZERO:
fragment = new DashBoardFragment();
title = fragment.getClass().getSimpleName();
break;
case Numerics.ONE:
break;
case Numerics.TWO:
break;
case Numerics.THREE:
break;
case Numerics.FOUR:
break;
default:
break;
}
if (fragment != null) {
showFragment(fragment, title);
}
setSelectedItem(position);
}
I'm adding fragments by adding to backstack in order to provide back navigation to previous fragment as follows :
if (fragment != null) {
String backStateName = fragment.getClass().getName();
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.dashboard_container, fragment);
mFragmentTransaction.addToBackStack(backStateName);
mFragmentTransaction.commit();
}
The problem is:
If I select Item no:3 and then select Item:4, if I navigate back the previous selection is still 4, but I'm displaying Fragment with no:3. So, if I select Item no:3 again from Navigation Drawer the Fragment loads again. How to solve this ??
EDIT:
public int getPrevSelectedItem() {
return selectedItem;
}
public void setSelectedItem(int selectedItem) {
this.selectedItem = selectedItem;
}
try this
Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.dashboard_container);
if(currentFragment instanceof YouFragmentName)
return
you can check the instance of loaded fragment on clicking the menu
Fragment loadedFragment= getFragmentManager().findFragmentById(R.id.fragmentLoadingSpace);
if( !(loadedFragment instanceof FragmentName1)){
// load fragment
}
I think there is no need of else statement or you can use else only for closing drawer
public int prevPosition=-1;
if (getPrevSelectedItem() != prevPosition) {
// Already selected fragment is being displayed
prevPosition =getPrevSelectedItem();
callFragment(position);
}
private void callFragment(int position) {
// TODO Auto-generated method stub
switch (position) {
case Numerics.ZERO:
fragment = new DashBoardFragment();
title = fragment.getClass().getSimpleName();
break;
case Numerics.ONE:
break;
case Numerics.TWO:
break;
case Numerics.THREE:
break;
case Numerics.FOUR:
break;
default:
break;
}
if (fragment != null) {
showFragment(fragment, title);
}
setSelectedItem(position);
}
From my comment:
The drawer does not get updated when you press back - so you need to add some code to onBackPressed() in the activity, where you update the position according to the popped BackstackEntry.
#Override
public void onBackPressed(){
String backStackEntryName = fragmentManager.getBackStackEntryAt(backCount - 2).getName();
int id = R.id.my_main_frag; //default option
if (MyMainFrag.class.getName().equals(backStackEntryName)){
id = R.id.my_main_frag;
selectedItem = 0;
} else if (MySecondFrag.class.getName().equals(backStackEntryName)){
id = R.id.my_second_frag;
selectedItem = 1;
}
...
navigation.setCheckedItem(id);
super.onBackPressed();
}
I am using the navigation drawer in Android Studio. When I select an item in navigation drawer I'm using the following code:
public void onNavigationDrawerItemSelected(int position) {
switch(position)
{
case 0:
Intent intent1 = new Intent(MainActivity.this,HomeActivity.class);
startActivity(intent1);
break;
case 1:
Intent intent2 = new Intent(MainActivity.this,DayActivity_1.class);
startActivity(intent2);
break;
}
}
When I call my activities from the navigation drawer item selected the action bar disappears and activities open on full screen. How can I manage that the navigation drawer doesn't disappear?
If you want to persist navigation drawer you should change content fragment, instead of showing Activity.
In your case it would be, change
Intent intent1 = new Intent(MainActivity.this,HomeActivity.class); startActivity(intent1);
with:
FragmentManager fragmentManager = ...
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.your_fragment_container_id, new HomeFragment())
transaction.commit();
dont use DayActivity_1.class activity use fragment instead and when you click on navigation item jst create fragment and replace it to the drawerlayout
Extend FragmentActivity in you main navigation activity class like below.
public class NavigationdrawerActivity extends FragmentActivity
Now use the below code to solve your problem
public void setContent(Fragment fragment) {
// Fragment fragment = new content_home();
FragmentTransaction fragmentManager = getFragmentManager().beginTransaction();
fragmentManager.setCustomAnimations(R.animator.enter_from_left, R.animator.exit_to_left);
// fragmentManager.beginTransaction()
fragmentManager.replace(R.id.mainContent, fragment).commit();
}
public void onNavigationDrawerItemSelected(int position) {
switch(position)
{
case 0:
break;
case 1:
Intent intent2 = new Intent(MainActivity.this,DayActivity_1.class);
startActivity(intent2);
break;
}
}
public void onNavigationDrawerItemSelected(int position) {
switch(position)
{
case 0:
Fragment homeActivityFragment = new HomeActivityFragment();
//if you want to pass data to fragment
//Bundle bundle = new Bundle();
//bundle.putString("id", "" + item.get("id"));
//homeActivityFragment.setArguments(bundle);
setContent(homeActivityFragment)
break;
case 1:
Fragment dayActivity_1Fragment = new DayActivity_1Fragment();
setContent(dayActivity_1Fragment)
break;
}
}
I'm using NavigationDrawer with some fragments, the problem is when I'm in a fragment and hit the back button, it makes the app close, then I have to open it again, put my username and password all over again to use the app, how can I prevent that from happen?
public class NavigationMain extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
}
public void setFragmentList(int posicao) {
Fragment fragment = null;
switch (posicao) {
case 0:
fragment = new MainFragment();
break;
case 1:
fragment = new MensagensFragment();
break;
case 2:
fragment = new EscolasFragment();
break;
case 3:
fragment = new AutorizadasFragment();
break;
case 4:
fragment = new CadastroFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
navigationAdapter.resetarCheck();
navigationAdapter.setChecked(posicao, true);
layoutDrawer.closeDrawer(linearDrawer);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
}
#Override
public void onBackPressed() {
int count = getFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
//additional code
} else {
getFragmentManager().popBackStack();
}
}
I think you missed to add the fragment transaction in your back stack. Try the following:
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null).commit();
I'm using the native Android drawer for my menu, here in MainActivity :
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
//Home
fragment = new EntryFragment();
break;
case 1:
//Journal
fragment = new JournalFragment();
break;
case 2:
// Medications
fragment = new RxFragment();
break;
case 3:
// Reminders
fragment = new ReminderFragment();
break;
case 4:
// Counselor Locator
fragment = new LocatorFragment();
break;
case 5:
// Library
fragment = new LibraryFragment();
break;
case 6:
// Settings
break;
case 7:
// About
break;
default:
break;
}
if (fragment != null) {
fm.beginTransaction()
.replace(R.id.container, fragment).addToBackStack(String.valueOf(position)).commit();
String fragmentTitles[] = getResources().getStringArray(R.array.menu_array);
if(mActionBar != null) {
mActionBar.setTitle(fragmentTitles[position]);
}
} else {
Log.e("MainActivity", "Error in creating fragment");
}
But I'm repurposing EntryFragment for another activity (EntryDetailActivity), so in EntryFragment I defined a couple of methods to modify the view based on which activity is hosting it:
// Runs on the home page, shows/hides appropriate views
public void prepareForHomeView() {
mTxtDate.setVisibility(View.GONE);
mBtnDiffDay.setVisibility(View.GONE);
mEdtNotes.setVisibility(View.GONE);
mBtnSave.setVisibility(View.GONE);
mBtnDelete.setVisibility(View.GONE);
}
// Runs on the detail page, shows/hides appropriate views
public void prepareForDetailView() {
mBtnLog.setVisibility(View.GONE);
}
In EntryDetailActivity, I can easily call prepareForDetailView() in onResumeFragments(), but I'm having an issue of where to call prepareForHome() in MainActivity, since I am swapping fragments in and out based on the menu. Any suggestions?
/** Update **/
I assigned the fragments a tag that is the position of the menu through addToBackstack(). I also added the following method to my MainActivity:
#Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
FragmentManager fm = getSupportFragmentManager();
EntryFragment entryFrag = (EntryFragment)fm.findFragmentByTag("0");
if(entryFrag != null) {
Log.v("rx", "Home frag is not null");
} else {
Log.v("rx", "Home frag is null");
}
}
Activity (or FragmentActivity) has onAttachFragment(Fragment fragment) - you could do an instanceof on fragment to determine if it is the correct type of fragment (or use the id, tag, etc) and call the appropriate method.
Alternatively, you could have the Fragment check the getActivity() (in onCreateView / onViewCreated).
This is one of the method() of Navigation Drawer activity...
Code Snippet:
/**
* Diplaying fragment view for selected nav drawer list item
**/
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ViewFragment();
break;
case 1:
//here i should call the FragmentActivity
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// Log.e("MainActivity", "Error in creating fragment");
}
}
create intent with fragment activity and use startActivity() to launch the fragment Activity.
switch (position) {
case 0:
fragment = new ViewFragment();
break;
case 1:
//here i should call the FragmentActivity
Intent intent = new Intent(Context, FragmentActivity.class);
startActivity(intent);
default:
break;
}
But make sure that your FragmentActivity. In FragmentActivity you have to use fragment to display the content.