I have an old project written in Java made to slide horizontally between three different fragments, and they were stated on the main activity. It worked perfect.
public class MainActivity extends AppCompatActivity {
ViewPager pager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = findViewById(R.id.theViewPagerInTheMainActivity);
pager.setOffscreenPageLimit(3);
PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
Frag_A aFrag = new Frag_A();
adapter.addItem(aFrag);
Frag_B bFrag = new Frag_B();
adapter.addItem(bFrag);
Frag_C cFrag = new Frag_C();
adapter.addItem(cFrag);
pager.setAdapter(adapter);
pager.setCurrentItem(1);
}
class PagerAdapter extends FragmentStatePagerAdapter {
ArrayList<Fragment> items = new ArrayList<Fragment>();
public PagerAdapter(FragmentManager fragManager) {
super(fragManager);
}
public void addItem(Fragment item) {
items.add(item);
}
#Override
public Fragment getItem(int position) {
return items.get(position);
}
#Override
public int getCount() {
return items.size();
}
}
}
Each three different fragments, followed by different codes that works fine, starts with:
public class Frag_A extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_a, container, false);
}
}
Same goes like Frag_B and frag_b layout, Frag_C and frag_c layout.
I'm doing it all again now, but in Kotlin and using ViewPager2. I'm not making any fragments programmatically, I want all three of their ID written on the main activity. Converting the code directly in Android Studio didn't work, of course, and hours of searching failed to cover my needs. From my humble understanding, creating different-coloured fragments from one pre-made fragment doesn't really seem to be adaptable for my project and all ended up in red underlines. Can it be made without importing .v4 supporting library?
Solved by myself!
class MainActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val adapter = DatPagerAdapter(this)
val fragsList = listOf(Frag_A(), Frag_B(), Frag_C())
adapter.fragsListHere.addAll(fragsList)
theVP2InTheMainActivity.adapter = adapter
theVP2InTheMainActivity.setCurrentItem(1)
}
class DatPagerAdapter(fragactivity: FragmentActivity) : FragmentStateAdapter(fragactivity) {
val fragsListHere = mutableListOf<Fragment>()
override fun getItemCount():Int = fragListHere.size
override fun createFragment(position: Int): Fragment = fragListHere[position]
}
}
Since FragmentPagerAdapter is depecrated. I switch it to FragmentStateAdapter(FragmentActivity fragmentActivity) that work fine with ViewPager2
My ViewPagerAdapter :
```
class ViewPagerAdapter(fragmentActivity: FragmentActivity):
FragmentStateAdapter(fragmentActivity){
override fun getItemCount(): Int {
return 3
}
override fun createFragment(position: Int): Fragment {
return when(position){
0 -> {
HomeFragment()
}
1 -> {
HomeFragment2()
}
2 -> {
HomeFragment3()
}
else -> {
throw Resources.NotFoundException("Position Not Found")
}
}
}
}
```
My Activity :
class MainActivity5 : AppCompatActivity() {
private lateinit var binding: ActivityMain5Binding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMain5Binding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.viewpager.adapter = ViewPagerAdapter(this)
}
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
override fun onBackPressed() {
if (binding.viewpager.currentItem == 0) {
super.onBackPressed()
} else {
// Otherwise, select the previous step.
binding.viewpager.currentItem = binding.viewpager.currentItem - 1
}
}
}
Thanks For asking!!
Note: I used binding to replace findViewById
Related
After OnCreateOptionsMenu() marked as deprecated, I've managed to use new API from release notes https://developer.android.com/jetpack/androidx/releases/activity#1.4.0-alpha01
In my app user can switch Fragments via bottomNavigation.
As I understand docs, in each Fragment I've implemented MenuProvider(with or without Lifecycle, doesn't matter for result). But now in each Fragment user have all items from all menuInflaters.
There is the code of implementation
FRAGMENT A
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
SetMainParams();
fragment = inflater.inflate( R.layout.fragment_A, container, false );
addMenu();
return fragment;
}
private void addMenu()
{
MenuProvider menuProvider = new MenuProvider()
{
#Override
public void onCreateMenu(#NonNull Menu menu, #NonNull MenuInflater menuInflater)
{
menuInflater.inflate(R.menu.menu_fragment_A, menu);
}
#Override
public boolean onMenuItemSelected(#NonNull MenuItem menuItem)
{
if( menuItem.getItemId() == R.id.filters_prev )
filtersPrevious();
else if( menuItem.getItemId() == R.id.filters )
showFilters();
else
filtersNext();
return false;
}
};
requireActivity().addMenuProvider(menuProvider, getViewLifecycleOwner(), Lifecycle.State.RESUMED);
}
FRAGMENT B
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
SetMainParams();
binding = FragmentBBinding.inflate(inflater, container, false);
fragment = binding.getRoot();
init();
addMenu();
return fragment;
}
private void addMenu()
{
MenuProvider menuProvider = new MenuProvider()
{
#Override
public void onCreateMenu(#NonNull Menu menu, #NonNull MenuInflater menuInflater)
{
menuInflater.inflate(R.menu.menu_fragment_B, menu);
filtersMenu = menu.getItem(0);
}
#Override
public boolean onMenuItemSelected(#NonNull MenuItem menuItem)
{
if( menuItem.getItemId() == R.id.filters )
loadFilters();
return false;
}
};
requireActivity().addMenuProvider(menuProvider, getViewLifecycleOwner(), Lifecycle.State.RESUMED);
}
Switching from bottomNavigation
binding.bottomNav.setOnItemSelectedListener(item ->
{
int itemId = item.getItemId();
if( itemId == R.id.A )
{
fm.beginTransaction().hide(active_fragment).show(A_fragment).commit();
active_fragment = A_fragment;
setWithElevation(false);
}
else if( itemId == R.id.B )
{
fm.beginTransaction().hide(active_fragment).show(B_fragment).commit();
active_fragment = B_fragment;
setWithElevation(true);
}
active_fragment.startFragment();
active_fragment.setTitle();
return true;
});
fm.beginTransaction().add( R.id.fl_content, A_fragment, "A_fragment" ).hide(A_fragment).commit();
fm.beginTransaction().add( R.id.fl_content, B_fragment, "B_fragment" ).hide(B_fragment).commit();
Is there any ideas, why new API works like this, or maybe i've made a mistake. Thanks a lot for help :)
I also experienced this problem today. So not sure if you have intellisense, but look at some of the Menu interface methods. I used the hasVisibleItems() method in Kotlin to only inflate the menu if it does not exist. Java also has the method; your snippet augmented with conditional:
MenuProvider menuProvider = new MenuProvider()
{
#Override
public void onCreateMenu(#NonNull Menu menu, #NonNull MenuInflater menuInflater)
{
if (!menu.hasVisibleItem()) { // only inflate if no items
menuInflater.inflate(R.menu.menu_fragment_A, menu);
}
}
//...
Menu also has a bunch of other methods that may assist for specific items.
Also another good idea would be to use the MenuHost interface to keep track of your MenuProvider. I believe in Java; inside your Fragments; it would be along the lines of:
activityFragment = this.requireActivity();
Menuhost menuHost = (MenuHost) activityFragment;
menuHost.addMenuProvider(
menuProvider,
this.getViewLifecycleOwner(),
State.RESUMED
);
Menu providers can then be invalidated ect.
I've a similar problem. In my application, I'm using fragments and navigation. The problem I found is that during navigation, the fragment never call the onPause event. So all my fragment implements this code in onViewCreated method:
class HomeFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
...
val menuHost: MenuHost = requireActivity()
menuHost.addMenuProvider(HomeMenuProvider(R.id.nav_main, findNavController(), this, authViewModel, homeViewModel), viewLifecycleOwner, Lifecycle.State.RESUMED)
}
The HomeMenuProvider is so defined:
class HomeMenuProvider(private val currentNavId: Int) : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
if ((navController.currentDestination?.id ?: -1L) == currentNavId) {
menu.clear()
menuInflater.inflate(R.menu.menu_main, menu)
}
}
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
return when (menuItem.itemId) {
R.id.menu_action_login -> {
...
true
}
else -> {
false
}
}
}
}
As you can see in my code, I add menu items only if the current navigation status is the one associated with the fragment. Otherwise, simply I do nothing.
To avoid menu persistence, before add items I clear the menu.
I hope this helps.
This is my Activity function that the startActivity(i) unable go to the Fragment page after scanning a barcode, I have try that it can go to activity page and show the code successful but I need it go to Fragment page.
barcodeDetector.setProcessor(object : Detector.Processor<Barcode> {
override fun release() {
Toast.makeText(applicationContext, "Scanner has been closed", Toast.LENGTH_SHORT)
.show()
}
override fun receiveDetections(detections: Detector.Detections<Barcode>) {
val barcodes = detections.detectedItems
if (barcodes.size() == 1) {
scannedValue = barcodes.valueAt(0).rawValue
runOnUiThread {
cameraSource.stop()
Toast.makeText(this#InsertStockInActivity, scannedValue, Toast.LENGTH_SHORT).show()
val i = Intent(this#InsertStockInActivity, comFragment::class.java)
.putExtra("cameraSource", scannedValue)
startActivity(i)
finish()
}
}else
{
Toast.makeText(this#InsertStockInActivity, "value- else", Toast.LENGTH_SHORT).show()
}
}
})
This is my Fragment page. do I write anything wrong?
class comFragment : Fragment() {
private lateinit var binding: FragmentComBinding
private val nav by lazy { findNavController() }
private val vm: StockInViewModel by activityViewModels()
private val formatter = SimpleDateFormat("dd MMMM yyyy '-' hh:mm:ss a", Locale.getDefault())
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentComBinding.inflate(inflater, container, false)
//binding.btnScanBarcode.setOnClickListener{ nav.navigate(R.id.insertStockInActivity) }
val value = requireActivity().intent.getStringExtra("cameraSource")
binding.edtId.findViewById<EditText>(R.id.value)
return binding.root
}
}
You use intent to navigate to Activity not to Fragment.
You can:
Use fragment transactions to navigate to new Fragment in your current Activity
Use intent to navigate to new Activity and in your new Activity use fragment transactions to your Fragment
You need to create a class that extends FragmentActivity and start your fragment there:
public class MyFragmentActivity extends YourActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new MyFragment ()).commit();}
}
}
then fragment constructor:
public MyFragment() {
}
then from your calling activity, start your fragment activity in the normal way
Intent i = new Intent(YourActivity.this, MyFragment.class);
startActivity(i);
I want to switch Fragments by button click inside the fragment itself. I have have created viewpager + tablayout, everything worked until i made an interface to communicate with my activity where i can set different Page for Viewpager. I am pretty new to Kotlin and I do not understand why I am catching NullPointer :(
I probably initialized my button poorly too, but I am not sure
Here is my MainActivity code
class MainActivity : AppCompatActivity()
, MyFragment1.buttonClick {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tablayout.addTab(tablayout.newTab().setIcon(R.drawable.ic_fragment1))
tablayout.addTab(tablayout.newTab().setIcon(R.drawable.ic_fragment_2))
tablayout.addTab(tablayout.newTab().setIcon(R.drawable.ic_fragment3))
tablayout.tabGravity = TabLayout.GRAVITY_FILL
val adapter = MyAdapter(this, supportFragmentManager, tablayout.tabCount)
viewpager!!.adapter = adapter
viewpager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tablayout))
val tabStrip = tablayout.getChildAt(0) as LinearLayout
for (i in 0 until tabStrip.childCount) {
tabStrip.getChildAt(i).setOnTouchListener { v, event -> true }
}
}
override fun buttonClicked(view: View) {
viewpager.currentItem = 2
}
fun selectIndex(index: Int) {
viewpager.currentItem = index
}
override fun onBackPressed() {
val currentPos = viewpager.currentItem
if (currentPos != 0) {
viewpager.currentItem = 0
} else {
super.onBackPressed()
}
}
}
**And a code for my Fragment**
class MyFragment1 : Fragment() {
private var click: buttonClick? = null
interface buttonClick {
fun buttonClicked(view: View)
}
var currentPage: Int = 0
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_my_fragment1, container, false)
val btn: Button = view?.findViewById(R.id.btn_next)
btn.setOnClickListener {
Toast.makeText(context, "test", Toast.LENGTH_LONG).show()
click?.buttonClicked(it)
}
return view
}
}
Have you initialized your click variable?
private var click: buttonClick? = null
override fun onAttach(context: Context?) {
super.onAttach(context)
click = context as? buttonClick
}
override fun onDetach() {
super.onDetach()
click = null
}
I want to call fragment method from another fragment, so I find relevant fragment in host activity and call method of this fragment. But some time I getting fragment null.
Suppose I want to save data of fragment A from all other fragment. There is 4 fragment like A, B, C and D. When I click save button from fragment A and B then it working fine but When I save data from fragment C and D then Fragment A getting null.
Here is my code :
HostActivity.cs
public class HostActivityView : MvxCachingFragmentCompatActivity<HostActivityViewModel>
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
try
{
SetContentView(Resource.Layout.HostActivityView);
var toolbar = FindViewById<Toolbar>(Resource.Id.myToolbar);
if (toolbar != null)
{
// Toolbar will now take on default actionbar characteristics
SetSupportActionBar(toolbar);
}
// View Pager
var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
if (viewPager != null)
{
// Add tabs in view pager
var fragments = new List<MvxFragmentStatePagerAdapter2.FragmentInfo>
{
new MvxFragmentStatePagerAdapter2.FragmentInfo("Ticket", typeof(Tab_Ticket), typeof(TicketEditViewModel)),
new MvxFragmentStatePagerAdapter2.FragmentInfo("Employee", typeof(Tab_Employee), typeof(EmployeeViewModel)),
new MvxFragmentStatePagerAdapter2.FragmentInfo("Response", typeof(Tab_Correspondence), typeof(ResponseViewModel)),
new MvxFragmentStatePagerAdapter2.FragmentInfo("Expense", typeof(Tab_Expenses), typeof(ExpenseViewModel)),
};
viewPager.Adapter = new MvxFragmentStatePagerAdapter2(this, SupportFragmentManager, fragments);
}
var tabLayout = FindViewById<TabLayout>(Resource.Id.tabs);
tabLayout.SetupWithViewPager(viewPager);
}
catch (Exception ex)
{
Mvx.Resolve<IUserInteraction>().Alert(ex.Message);
}
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Resource.Id.menu_accept:
Tab_Ticket tabTicket = (Tab_Ticket)SupportFragmentManager.FindFragmentByTag("Tab_Ticket");
if (tabTicket != null)
{
tabTicket.OnOptionsItemSelected(item);
handled = true;
}
break;
}
}
}
Here is my fragment Code :
Fragment.cs :
public class Tab_Ticket : MvxFragment<TicketEditViewModel>
{
public override View OnCreateView(Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.frg_tab_ticket, null);
return view;
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Resource.Id.menu_accept:
this.ViewModel.CmdTicketSave.Execute(null);
return true;
break;
}
}
}
Please suggest me where I going wrong?
Thank you
Finally got the solution for this problem.
In host activity increase the view pager page limit. Because I have 5 fragments and default it display 3 fragments in support fragment manager. So sometime getting first fragment null. Added this line in my code and it working fine.
viewPager.OffscreenPageLimit = 6;
Here is more details about this issue.
This is ActivityUtil code
public class ActivityUtil {
public static void addFragmentToActivity(#NonNull FragmentManager fragmentManager,
#NonNull Fragment fragment, int frameId, String fragmentTag) {
//Fragment fragment1=fragmentManager.findFragmentByTag(fragmentTag);
Preconditions.checkNotNull(fragmentManager);
Preconditions.checkNotNull(fragment);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(frameId, fragment, fragmentTag);
transaction.addToBackStack(fragmentTag);
transaction.commit();
}
}
This is main fragment class here i want add /replace fragment transaction by using kotlin fragment i.e,CameraFragment Class
I Already used Photofragment class now i want change the kotlin fragment, how do achieve this scenario?
public class ExpLotBcodeFragment extends Fragment{
public ExpLotBcodeFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRetainInstance(true);
}
#SuppressLint("ClickableViewAccessibility")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_exp_lot_bcode, container, false);
}
#OnClick({R.id.scan})
public void onClick(View view) {
switch (view.getId()) {
case R.id.scan:
//here i want to add kotlin call CameraFragment class
//This is old call
ActivityUtil.addFragmentToActivity(getFragmentManager(),
photoFragment, R.id.frame_content, "photoFragment");
break;
}
This is kotlin class
class CameraFragment : Fragment() {
companion object {
fun newInstance(): CameraFragment {
return CameraFragment ()
}
}
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_exp_lot_bcode, container, false)
}
}
From CameraFragment class i want to add/replace to ExpLotBcodeFragment class ?
Either you have to call CameraFragment.Companion.newInstance() or directly call new CameraFragment() to get instance of CameraFragment.
Check below:
#OnClick({R.id.scan})
public void onClick(View view) {
switch (view.getId()) {
case R.id.scan:
CameraFragment cameraFragment = CameraFragment.Companion.newInstance();
ActivityUtil.addFragmentToActivity(getFragmentManager(),
cameraFragment, R.id.frame_content, "cameraFragment");
break;
}
}