i have 3 fragments inside an activity, it's a swipe menu with tabs viewpager
how to get into second fragment from another activity ?
what i mean by second fragment is when iam using this standart intent
Intent i = new Intent(getBaseContext(),Form_Mhs_04.class);
startActivity(i);
it went to first intent (case 0)
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position){
case 0 :
fragment = new form_mhs_04_fragment1();
break;
case 1 :
fragment = new form_mhs_04_fragment2();
break;
case 2 :
fragment = new form_mhs_04_fragment3();
}
return fragment;
}
You can't set 2nd fragment in TabLayout in this way. You have to pass the position that you want to select using intent.
Intent i = new Intent(this, Form_Mhs_04.class);
i.putExtra("TAB_POSITION", 2);
startActivity(i);
Then inside your Form_Mhs_04 activity get this position from intent and set TabLayout selected tab based on that.
int position = getIntent().getIntExtra("TAB_POSITION", 0);
TabLayout.Tab tab = tabLayout.getTabAt(position);
if(tab != null)
tab.select();
Related
I am using a fragment to hold 4 card views, which should start a new activity on click. It can display the toast I generate which shows that the click function is working but it is unable to activate the intent.
I have tried using several names for the activity and also
Intent intent = new Intent (getActivity(), activity name.class)
code to start activity
package com.example.eb.ui.home;
import ...
public class HomeFragment extends Fragment implements View.OnClickListener{
private HomeViewModel homeViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
final CardView java = root.findViewById(R.id.javacardId);
final CardView html = root.findViewById(R.id.htmlcardId);
final CardView c_prog = root.findViewById(R.id.C_cardId);
final CardView cpp = root.findViewById(R.id.cppcardId);
//set on click listener
java.setOnClickListener(this);
html.setOnClickListener(this);
cpp.setOnClickListener(this);
c_prog.setOnClickListener(this);
return root;
}
#Override
public void onClick(View v) {
Intent i;
switch(v.getId()){
case R.id.javacardId :
i = new Intent(this,html.class);
startActivity(i);
break;
case R.id.htmlcardId :
i = new Intent(this,html_prog.class);
startActivity(i);
break;
case R.id.cppcardId :
i = new Intent(this,cpp_prog.class);
startActivity(i);
break;
case R.id.C_cardId :
i = new Intent(this,c_prog.class);
startActivity(i);
break;
default: break;
}
}
}
---
I expect the card view in that fragment to open up new activity
Try changing the "this" keyword to getContext()
"i = new Intent(this,html.class);"
should be
i = new Intent(getContext,html.class);
or initialize and set the clicklistener in onViewCreated() lifecycle method of Fragment.
You have to use getActivity() instead of 'this' in a fragment for context like below.
#Override
public void onClick(View v) {
Intent i;
switch(v.getId()){
case R.id.javacardId :
i = new Intent(getActivity(),html.class);
startActivity(i);
break;
default: break;
}
The first parameter required by Intent is Context
As mentioned in the Android documentation (Here):
packageContext Context: A Context of the application package implementing this class.
Hence, you need to pass getContext() or getActivity() (since Activity extends Context) as follows:
i = new Intent(getActivity(), html.class);
startActivity(i);
I tried building and launching an app using your code (after making the change above) and it worked fine.
I have this code:
public void replaceFragment(int i) {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
TextView headerName = (TextView) findViewById(R.id.HeaderName);
switch (i) {
// Login Fragment
case 0:
headerName.setText("Member Login");
fragmentTransaction.replace(R.id.main_container, new
LoginActivity());
break;
// Register Fragment
case 1:
headerName.setText("Sign Up");
fragmentTransaction.replace(R.id.main_container, new RegisterFragment());
break;
}
}
My problem is on the case 0, I put the other cases as examples. My login is not a Fragment, but an Activity. So how do I manage to keep it here? With an Intent?
Instead of
fragmentTransaction.replace(R.id.main_container, new
LoginActivity());
Use
Intent inntent = new Intent(this,LoginActivity.class);
startActivity(inntent);
Just build a new intent in case 0 and start your activity instead of trying to replace a fragment with activity which is impossible unless you turn your activity to fragment
If you need both you can turn your activity to fragment and then build an empty activity to contain it when necessary
My problem is that I am using NavigationDrawer with multiple fragments. I have an Activity. I am trying to return from the activity to Specific Fragment. I have tried to send an intent. It works but it always stay in that fragment due to the bundle value is not null. So if you have a way how to make the bundle null or you have a better solution than the intent I will be grateful. Thanks, all.
This Method is capable of switching the Fragments
public void DisplaySelectedScreen(int id) {
infoNom = this.getIntent().getExtras();
if (infoNom != null) {
id = Integer.valueOf(infoNom.getString("Fragment"));
}
infoNom = null ; //not Working
Fragment fragment = null;
switch (id) {
case R.id.nav_home:
fragment = new Home();
break;
case R.id.nav_info:
fragment = new Informations();
break;
case R.id.nav_cmd:
fragment = new Commandes();
break;
case R.id.nav_panier:
fragment = new Cart();
break;
case R.id.nav_contact:
fragment = new Contact();
break;
case R.id.nav_about_us:
fragment = new About_Us();
break;
case R.id.nav_logout:
ParseUser.logOut();
Intent intent = new Intent(Main.this, Welcome.class);
startActivity(intent);
break;
}
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
You can clear the bundle using below method:
getIntent().removeExtra("key");
Hope It help you !
Ways to replace extras
void removeExtra(String name)
Remove extended data from the intent.
void removeFlags(int flags)
Remove these flags from the intent.
Intent replaceExtras(Intent src)
Completely replace the extras in the Intent with the extras in the given Intent.
Intent replaceExtras(Bundle extras)
Completely replace the extras in the Intent with the given Bundle of extras.
Hope this helps
I am trying to figure out how to navigate from an activity to a fragment that is hosted by a separate activity.
Even more complex is the destination activity hosts a TabLayout / ViewPager with 3 separate fragments. Here is the code for the adapter associated with the TabLayout / ViewPager. I am trying to navigate to the "NewFragment" section of the TabLayout / ViewPager:
public class SectionPagerAdapter extends FragmentPagerAdapter {
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new TrendingFragment();
case 1:
return new FollowingFragment();
case 2:
return new NewFragment();
default:
return new TrendingFragment();
}
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getResources().getString(R.string.trending_text);
case 1:
return getResources().getString(R.string.following_text);
case 2:
return getResources().getString(R.string.new_text);
default:
return getResources().getString(R.string.trending_text);
}
}
}
I would think I would have to use an intent, but where I get confused is how I would ensure that the activity loads the NewFragment instead of the first fragment in the tab, the TrendingFragment.
You could try with something like this.
In the first activity:
int position = the fragment position in the target activity you want to open;
Intent intent = new Intent(getActivity(), TargetActivity.class);
intent.putExtra("fragmentPosition", position);
startActivity(intent);
Int the second activity, after view pager initialization:
Intent intent = getIntent();
int fragmentPosition = intent.getIntExtra("fragmentPosition");
viewPager.setCurrentItem(fragmentPosition);
Hope it helps
from the adapter of a RecyclerView which is contained in an Activity, i'm trying to launch a fragment when an element of the RecyclerView is pressed, this is my code right now :
#Override
public void onClick(View v) {
Intent intent;
int position = getAdapterPosition();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String onoff = preferences.getString("it's", "");
if (onoff.equalsIgnoreCase("on")) {
if (position == 0) {
CategoriesRecyclerView.fragSubreddit = "aww";
intent = new Intent(context, MainFragment.class);
context.startActivity(intent);
}
if (position == 1) {
CategoriesRecyclerView.fragSubreddit = "food";
intent = new Intent(context, MainFragment.class);
context.startActivity(intent);
}
if (position == 2) {
CategoriesRecyclerView.fragSubreddit = "funny";
intent = new Intent(context, MainFragment.class);
context.startActivity(intent);
}
if (position == 3) {
CategoriesRecyclerView.fragSubreddit = "gaming";
intent = new Intent(context, MainFragment.class);
context.startActivity(intent);
}
} else if (onoff.equalsIgnoreCase("off")) {
if (position == 0) {
CategoriesRecyclerView.fragSubreddit = "gaming";
intent = new Intent(context, MainFragment.class);
context.startActivity(intent);
}
if (position == 1) {
CategoriesRecyclerView.fragSubreddit = "funny";
intent = new Intent(context, MainFragment.class);
context.startActivity(intent);
}
if (position == 2) {
CategoriesRecyclerView.fragSubreddit = "food";
intent = new Intent(context, MainFragment.class);
context.startActivity(intent);
}
if (position == 3) {
CategoriesRecyclerView.fragSubreddit = "aww";
intent = new Intent(context, MainFragment.class);
context.startActivity(intent);
}
}
}
I Tested it launching some "testing activities" that i created, so i know that everything but the fragment launching works fine.
The error is here :
intent = new Intent(context, MainFragment.class);
context.startActivity(intent);
I'm launching the Fragment as it was an Activity, so when i run the app it crashes and tells me to declare the MainFragment as an activity in my manifest.
How can i launch that Fragment from my Activity?
Thanks very much.
You cannot start a Fragment with Intents so the method that I recommend to you is :
Fragment mFragment = null;
mFragment = new MainFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
For more information see Fragments Transactions documents
You can not start a Fragment using Intents. Intents provide a communication between activities.
If you want to launch a Fragment from other Fragment you have two options.
1- You can replace or add a new fragment.
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction
.replace( R.id.content_frame, fragment )
.addToBackStack( null )
.commit();
In this case The activity has two fragments.
2- You can launch a new Activity that contains a Fragment.
If you are using a Sections Pager Adapter, the simple thing to do would be using an onclick on image view or button like below:
In your starting activity (from where you want to go) use put extra to send some information to another activity
ivPhotoAlbums.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Send The Message Receiver ID & Name
Intent intent = new Intent(v.getContext(), PhotosActivity.class);
intent.putExtra("viewpager_position", 1);
v.getContext().startActivity(intent);
}
});
In your activity in which you want to land (at the end of your setupViewPager() function) catch that extra and use it as position
int position = 0;
Bundle extras = getIntent().getExtras();
if (extras != null) {
position = extras.getInt("viewpager_position");
}
viewPager.setCurrentItem(position);
Given the Assumption that your setupViewPager() function looks like below in your destination activity
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FriendsAlbumsFragment()); //index 0
adapter.addFragment(new MyAlbumsFragment()); //index 1
adapter.addFragment(new AddPhotosFragment()); //index 2
final ViewPager viewPager = findViewById(R.id.l_center_view_pager_view_pager);
viewPager.setAdapter(adapter);
TabLayout tabLayout = findViewById(R.id.l_top_tabs_tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setIcon(R.drawable.ic_friends_photos);
tabLayout.getTabAt(1).setIcon(R.drawable.ic_my_photos);
tabLayout.getTabAt(2).setIcon(R.drawable.ic_add_photos);
And Your SectionsPagerAdapter class looks like below (separate public class)
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return mFragmentList.get(i);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment) {
mFragmentList.add(fragment);
}
}
And your center view pager layout is
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.view.ViewPager
android:id="#+id/l_center_view_pager_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="56dp">
</android.support.v4.view.ViewPager>
</merge>
Included in another file like this (or not included - in my case it was included)
<include layout="#layout/layout_center_viewpager" />
This will take you right from an activity to a fragment (in another activity) or from a fragment in one activity to a fragment in another activity...