I have a sliding menu with some Fragments, but i want to change it with Activities, is it possible?
private void replaceFragment(int pos) {
Fragment fragment = null;
switch (pos) {
case 0:
fragment = new X();
break;
case 1:
fragment = new Y();
break;
case 2:
fragment = new Z();
break;
default:
fragment = new X();
break;
}
if (null != fragment) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_content, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
I don't know how to change the last part of the code. --> FragmentManager and --> FragmentTransaction. Thanks!!
Yes you can use Activity in place of fragment, But
you have following challenges.
You need to implement sliding drawer on every activity, if you uses fragment then HomeActivity sliding drawer will be shown on all fragment
update your code like this if you want to implement activity
private void replaceFragment(int pos) {
Intent intent = null;
Context context = this;
switch (pos) {
case 0:
intent = new Intent(context, SecondActivity.class);
break;
case 1:
intent = new Intent(context, ThirdActivity.class);
break;
case 2:
intent = new Intent(context, FourthActivity.class);
break;
default:
intent = new Intent(context, DefaultActivity.class);
break;
}
if (intent != null) {
startActivity(intent);
}
}
Related
i'm using a navigation drawer to switch between fragments , each time i move to a new one ,it's completely recreated and the previous one is completely destroyed, so i want to conserve the state of fragments while swapping and back again
Runnable mPendingRunnable = new Runnable() {
#Override
public void run() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
Fragment fragment=getHomeFragment();
fragmentTransaction.replace(R.id.frame,fragment,CURRENT_TAG);
fragmentTransaction.commitAllowingStateLoss();
}
};
if (mPendingRunnable != null) {
mHandler.post(mPendingRunnable);
}
and the getHomeFragment() :
public Fragment getHomeFragment() {
switch (navItemIndex) {
case 0:
HomeFragment homeFragment = new HomeFragment();
return homeFragment;
case 1:
BoiteRFragment boiteRFragment = new BoiteRFragment();
return boiteRFragment;
case 2:
InfoPersFragment infoPersFragment = new InfoPersFragment();
return infoPersFragment;
case 3:
NotificationsFragment notificationsFragment = new NotificationsFragment();
return notificationsFragment;
case 4:
PrametresFragment prametresFragment = new PrametresFragment();
return prametresFragment;
default:
return new HomeFragment();
}
}
I just want to call fragment from this class.
Actually i have many fragments in my app and have to call them again and again.
So i thought to make a class and a function for loading a fragment so whenever i need to call a fragment i can use the function of this class.
But iam unable to get getSupportFragmentManager() here.
I tried by extending the class to fragment but then it produces null exception.
also by extending with Appcompactactivity and uses getSupportFragmentManager(); but is also gives error by saying the activity destroyed.
So anyone have solution to call a fragment from a simple class?
public class CompletedandPendingScreensLoader {
public void pendingscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("pending","pen");
frag.setArguments(bundle);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void completedscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("completed","yes");
frag.setArguments(bundle);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void simpleScreenLoader( int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
FragmentTransaction transaction = getFragmentManager.beginTransaction();
transaction.replace(R.id.frame, new LessonTwo()).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit();
}
}
I believe you will at some point call this class from some activity or fragment if so then use the modified constructor it will open the fragment. else you can't do it without having a reference for FragmentManager from activity or fragment.
public class CompletedandPendingScreensLoader {
private FragmentManager fragmentManager = null;
//when ever you start your class just start using this constructor
CompletedandPendingScreensLoader(FragmentManager fragmentManager){
this.fragmentManager = fragmentManager
}
public void pendingscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("pending","pen");
frag.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void completedscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("completed","yes");
frag.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void simpleScreenLoader( int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame, new LessonTwo()).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit();
}
}
Your class must extend AppCompatActivity class to call this function, and when you extend the class then you have to also override onCreate() method of the activity and you have to also set Layout for the activity in onCreate() Method
public class CompletedandPendingScreensLoader {
private AppCompatActivity myActivty;
public CompletedandPendingScreensLoader(AppCompatAcitivity myActivty)
{
this.myActivity = myActivity
}
public void pendingscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("pending","pen");
frag.setArguments(bundle);
FragmentTransaction transaction =
myActivity.getFragmentManager().beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void completedscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("completed","yes");
frag.setArguments(bundle);
FragmentTransaction transaction =
myAcitvity.getFragmentManager().beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void simpleScreenLoader( int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
FragmentTransaction transaction =
myActivity.getFragmentManager.beginTransaction();
transaction.replace(R.id.frame, new LessonTwo()).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit();
}
}
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 have a code in which it opens five fragment class,but i have one case to call activity class in my project.
Can I change case 3 to call activity class instead of fragment class?
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;
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 {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
If your questions is whether you can call new MyActivity(), the answer is no. Only the Android framework can instantiate an Activity and it does this based on an Intent being used in a call to Context.startActivity().
If you mean by calling is starting the new activity, then small answer is YES, you can do that.
But if you mean to instantiate is then; No, you can't do that.
switch (position) {
case 0:
*******WRONG WAY*******
myAct = new MyActivity(); // Where MyActivity is extended from Activity
break;
case 1:
*******RIGHT WAY*******
Intent intent=new Intent(MyCurrentActivity.this, MyNewActivity.class);
startActivity(intent);
break;
}
What i have is left navigation menu that i have made using navigation drawer and it works just fine with fragments , and i have five buttons each one open a fragment but now i want each case to open an activity instead of fragment , and i have tried to do it using intent but it didn't work !!
here is my code :
private void displayView(int position) {
// update the main content by replacing fragments
android.app.Fragment fragment = null;
switch (position) {
case 0:
//fragment = new HomeFragment();
// Intent i=new Intent(MainActivity.this,MainActivity.class);
// startActivity(i);
break;
case 1:
//fragment = new FindPeopleFragment();
break;
case 2:
//fragment = new PhotosFragment();
break;
case 3:
//fragment = new CommunityFragment();
break;
case 4:
//fragment = new PagesFragment();
break;
case 5:
//fragment = new WhatsHotFragment();
break;
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 {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
So what shall i change in this code so it can open an activity instead of fragment??? can anyone help me?
You need to change your function to load a class Fragment in some case and in other cases a class Activity
case 0:
activity = new MyActivity();
fragment = null;
break;
case 1:
fragment = new MyFragment();
activity = null;
break;
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, (android.app.Fragment) fragment).commit();
setTitle(menutitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}else {
if(activity != null) {
Intent i1 = new Intent(MainActivity.this, activity.getClass());
i1.putExtra(EXTRA_MESSAGE, position);
startActivity(i1);
setTitle(menutitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
Could you please post the Error from Logcat:
What i would check first is:
Are the Activities declared in the Manifest.xml as Activies?
Secondly check if you calling Activities with an Intent, and not Fragments
This is how you start Activities correctly.
Intent intent = new Intent(mActivity, class1); // class1 The class to open
startActivity(intent);