Animated (textView) keeps cancelling when swiping between fragments within Fragment State Adapter - java

I am developing an app on android studio (Java)
I have created a tabLayout that swipes between two fragments using a viewPager2 + Fragment State Adapter
On fragment02, I have animated a textView.
When I swipe onto the fragment the animation plays on loop, as it should.
However, if I swipe off the fragment and then back on, the animation is no longer playing.
I really need the animation to continue.
Any advice is welcome and I thank you in advance :)
Please be aware I am rather new to this.
// MainActivity.java
package Zoomie.App;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("Welcome");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ViewPager2 viewPager2 = findViewById(R.id.ViewPager);
viewPager2.setAdapter(new FragmentPagerAdapter(this));
TabLayout tabLayout = findViewById(R.id.tabLayout);
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
#Override
public void onConfigureTab(#NonNull TabLayout.Tab tab, int position) {
switch (position) {
case 0: {
tab.setText("Fragment 01");
break;
}
default: {
tab.setText("Fragment 02");
break;
}
}
}
});
tabLayoutMediator.attach();
}
}
// FragmentPagerAdapter.java
package Zoomie.App;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
public class FragmentPagerAdapter extends FragmentStateAdapter {
public FragmentPagerAdapter(#NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
#NonNull
#Override
public Fragment createFragment(int position) {
switch (position){
case 0:
return new Fragment01();
default:
return new Fragment02();
}
}
#Override
public int getItemCount() {
return 2;
}
}
// Fragment02.java
package Zoomie.App;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
public class Fragment02 extends Fragment {
public Fragment02() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.fragment_02, container, false);
TextView fade01 = (TextView) rootView.findViewById(R.id.textView_fade_01);
Animation fade = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_animation);
fade01.startAnimation(fade);
return rootView;
}
}
// fade_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0"
android:toAlpha=".5"
android:duration="375"
android:repeatMode="reverse"
android:repeatCount="infinite"
/>
</set>

You should move the loading animation method into onResume() lifecycle method. In your case:
private TextView fade01;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState
) {
View rootView = inflater.inflate(R.layout.fragment_02, container, false);
fade01 = (TextView) rootView.findViewById(R.id.textView_fade_01);
return rootView;
}
#Override
public void onResume() {
super.onResume();
Animation fade = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_animation);
fade01.startAnimation(fade);
}

Related

Android Studio: Unable to remove fragment

I'm trying to remove a fragment and it's not working.
I've been checking with logcat and it's noticing that the button's been clicked and that message is being passed to the main activity. I assume there's something wrong with my code but I can't figure out what that is. Would appreciate some help.
MainMenu.java
package com.example.android.learninstrumentation;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainMenu extends AppCompatActivity implements MainMenuStFrag.MainMenuStFragListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
//Create a new Fragment to be placed in the Activity layout
Fragment menuSiFragment = new MainMenuSiFrag();
Fragment menuStFragment = new MainMenuStFrag();
//Add the fragment to the 'fragment_container' layout
getSupportFragmentManager().beginTransaction().add(R.id.fragment_type_si_container, menuSiFragment).commit();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_type_st_container, menuStFragment).commit();
}
//The user clicked 'expand' from MainMenuStFrag. Remove MainMenuSiFrag.
public void onExpandClick () {
Log.i("MainMenu", "Click message received");
Fragment oldFragment = new MainMenuSiFrag();
getSupportFragmentManager().beginTransaction().remove(oldFragment).addToBackStack(null).commit();
}
public void onCheckboxClick() {
}
}
MainMenuStFrag.java
package com.example.android.learninstrumentation;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
public class MainMenuStFrag extends Fragment implements View.OnClickListener{
MainMenuStFragListener mCallback;
//Inflate the layout and set buttons for this fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Store the Fragment in the mainMenu variable
View mainMenu = inflater.inflate(R.layout.main_menu_st_fragment, container, false);
//Define the buttons and set onClickListeners
ImageView expand = mainMenu.findViewById(R.id.mainMenu_imageView_SelectTopics);
expand.setOnClickListener(this);
CheckBox checkBox = mainMenu.findViewById(R.id.mainMenu_CheckBox_SelectAllTopics);
checkBox.setOnClickListener(this);
return mainMenu;
}
//Interface for communication with container Activity
public interface MainMenuStFragListener {
void onExpandClick();
void onCheckboxClick();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
//This makes sure the container Activity has implemented the callback interface.
// If not, it throws an exception.
try {
mCallback = (MainMenuStFragListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
//onClick actions
#Override public void onClick(View v) {
switch (v.getId()) {
//Remove the other 'type' item
case R.id.mainMenu_imageView_SelectTopics:
Log.i("MainMenuStFrag", "Topics>Expand=Clicked");
mCallback.onExpandClick();
break;
case R.id.mainMenu_CheckBox_SelectAllTopics:
mCallback.onCheckboxClick();
break;
}
}
}
MainMenuSiFrag.java
package com.example.android.learninstrumentation;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainMenuSiFrag extends Fragment {
//Inflate the layout for this fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mainMenu = inflater.inflate(R.layout.main_menu_si_fragment, container, false);
return mainMenu;
}
}
You have created new instance of Fragment for remove Fragment
public void onExpandClick () {
Log.i("MainMenu", "Click message received");
Fragment oldFragment = new MainMenuSiFrag();
getSupportFragmentManager().beginTransaction().remove(oldFragment).addToBackStack(null).commit();
}
instead on that Find a fragment and remove it from container
Fragment oldFragment=getSupportFragmentManager().findFragmentById(R.id.fragment_type_si_container);
if(oldFragment!=null && oldFragment instanceof MainMenuSiFrag)
{
getSupportFragmentManager()
.beginTransaction().
remove(oldFragment).commit();
}

Incompatible types: DetailOneFragment cannot be converted to Fragment

I have problem in my code
error is
incompatible types: DetailOneFragment cannot be converted to Fragment
in Android
incompatible types: DetailTwoFragment cannot be converted to Fragment
in Android
Which is wrong?
this is DetailActivity.java
import android.content.Intnet;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import org.adnroidtown.albaplanet.R;
import org.adnroidtown.albaplanet.review_view.Review1Activity;
public class DetailActivity extends AppCompatActivity {
ViewPager pager;
TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
//메인 액티비티에서 넘겨받은 인텐트 -시작-
ImageView iv = (ImageView) findViewById(R.id.detail_img);
TextView tvName = (TextView) findViewById(R.id.detail_store);
Intent intent = getIntent();
iv.setImageResource(intent.getIntExtra("img", 0));
tvName.setText(intent.getStringExtra("name"));
pager = (ViewPager) findViewById(R.id.detail_viewpager);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
FragmentManager manager = getSupportFragmentManager();
PagerAdapter adapter = new PagerAdapter(manager);
pager.setAdapter(adapter);
tabLayout.setupWithViewPager(pager);
// mTabLayout.setupWithViewPager(mPager1);
pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setTabsFromPagerAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {#Override
public void onClick(View view) {
Intent intent = new Intent(DetailActivity.this, Review1Activity.class);
startActivity(intent);
}
});
}
}
this is PagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment frag = null;
switch (position) {
case 0:
frag = new DetailOneFragment();
break;
case 1:
frag = new DetailTwoFragment();
break;
}
return frag;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
String title = " ";
switch (position) {
case 0:
title = "Game";
break;
case 1:
title = "Movie";
break;
}
return title;
}
}
this is DetailOneFragment.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.adnroidtown.albaplanet.R;
public class DetailOneFragment extends Fragment {
public DetailOneFragment(){
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_detail_one, container, false);
}
}
This is error ScreenShot
See your screenshot. You Import/extend android.app.Fragment;in DetailOneFragment and use android.app.support...Fragment in your other class
replace
import android.app.Fragment;
with
import android.support.v4.app.Fragment;
in your fragments
As of 02-2021 the solution above did work for me but i had to replace :
import android.app.Fragment;
with
import androidx.fragment.app.Fragment;
instead

Android Studio Swipe to different views

I'm new beginner using Android studio I have currently watched a couple tutorials on setting up a basic swipe view which generates up to 3 all which replicate the original page known in my code as page_fragment_layout.xml. I want to take this a step further a be able to link different pages containing a range of content. In this case I want to be able to link my Activity_main.xml and page_fragment_layout.xml together by swipe. I have added my code so far I would be most appreciative of any input.
MainActivity.java
package socialdeveloper.swipecard;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.view_pager);
SwipeAdapter swipeAdapter = new SwipeAdapter(getSupportFragmentManager());
viewPager.setAdapter(swipeAdapter);
}
}
PageFragment.java
package socialdeveloper.swipecard;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {#link Fragment} subclass.
*/
public class PageFragment extends android.support.v4.app.Fragment {
TextView textView;
public PageFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.page_fragment_layout,container,false);
textView = (TextView)view.findViewById(R.id.TestText);
Bundle bundle = getArguments();
String message = Integer.toString(bundle.getInt("count"));
textView.setText("This is the "+message+ "Swipe View Page...");
return view;
}
}
SwipeAdapter.java
package socialdeveloper.swipecard;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
/**
* Created by Hadleigh on 07/12/2015.
*/
public class SwipeAdapter extends FragmentStatePagerAdapter {
public SwipeAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new PageFragment();
Bundle bundle = new Bundle();
int i = 0;
bundle.putInt("count",i+1);
fragment.setArguments(bundle);
return fragment;
}
#Override
public int getCount() {
return 3;
}
}
in your SwipeAdapter.java use this
` public Fragment getItem(int position) {
Fragment fragment = null;//Creates a fragment variable to hold the fragment class i create and make it a null so i can catch the error
if (position == 0) {
fragment = new PUT_THE_TITLE_OF_YOUR_ACTIVITY_YOU_WANT_TO_USE();//Calls the fragment.
}
if (position == 1) {
fragment = new CALL_A_DIFFERENT_FRAGMENT_OR_ACTIVITY();//Calls the fragment;
}
if (position == 2) {
fragment = new CALL_A_DIFFERENT_FRAGMENT_OR_ACTIVITY();//Calls the fragment.
}
return fragment;
}`
in the getItem() method to call other fragments or activities.

Switching to a different fragment while staying in the same tab in a Sliding Tab Layout

I have a Sliding Tab layout with a Recycler View that holds three tabs. The first tab holds a Card View. What is the best way to achieve the following: when you click on a button inside the first card the Card View (a fragment) it is replaced by another Card View (another fragment) while staying in the same tab.
I know I have to create a new fragment and use transaction. I just do not know where. Do I put it in an adapter? If possible I would like to use onClick for the button in the XML Layout for the first Card View.
Sliding Tab How-to
Sliding Tab Layout
Sliding Tab Strip
MainActivity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new MainFragmentPageAdapterForTabs(getSupportFragmentManager(),
MainActivity.this));
// Give the SlidingTabLayout the ViewPager
SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
// Center the tabs in the layout
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setViewPager(viewPager);
}
public void bodyButtonAction(View view){
Intent intentBody = new Intent(MainActivity.this, MainActivity.class);
startActivity(intentBody);
}
Tab Fragments:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
// In this case, the fragment displays simple text based on the page
public class MainPageFragmentForTabs extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static MainPageFragmentForTabs newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
MainPageFragmentForTabs fragment = new MainPageFragmentForTabs();
//body fragment
Fragment bodyFragment = new MainPageFragmentForTabs();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = null;
if(mPage==1){
View view1 = inflater.inflate(R.layout.main_fragment_page, container, false);
FragmentActivity a = getActivity();
//recycler
RecyclerView recyclerView = (RecyclerView) view1.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
//layout manager
LinearLayoutManager manager = new LinearLayoutManager(a);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
MainAdapterCV1 ca = new MainAdapterCV1();
recyclerView.setAdapter(ca);
view=view1;
}
if(mPage==2){
View view2 = inflater.inflate(R.layout.main_fragment_page, container, false);
//stand-in code
TextView textView = (TextView) view2;
textView.setText("Fragment #" + mPage);
//stand-in code
view=view2;
}
if(mPage==3){
View view3 = inflater.inflate(R.layout.main_fragment_page, container, false);
//stand-in code
TextView textView = (TextView) view3;
textView.setText("Fragment #" + mPage);
//stand-in code
view=view3;
}
return view;
}
}
Adapter for first Card View:
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainAdapterCV1 extends RecyclerView.Adapter<MainAdapterCV1.CardViewHolder> {
//
#Override
public int getItemCount() {
return 1;
}
#Override
public void onBindViewHolder(CardViewHolder cardViewHolder, int i) {
}
//
#Override
public CardViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.main_card_view1, viewGroup, false);
return new CardViewHolder(itemView);
}
public static class CardViewHolder extends RecyclerView.ViewHolder {
public CardViewHolder(View v) {
super(v);
}
}
}
Technically you can add or replace a fragment anywhere you want. However doing it in the Adapter, I think, is more logical than inside a fragment. One reason is the adapter normally has the click event listener code, and I don't see it in the posted code, which is fine. Sample code to replace a fragment (using your code):
MainPageFragmentForTabs myFragment = MainPageFragmentForTabs.newInstance(page);
FragmentTransaction transaction = thisActivity.getFragmentManager().beginTransaction();
// Replace the Fragment (set in MainActivity) with this fragment.
transaction.replace(R.id.sample_content_fragment, myFragment, "MainPage");
transaction.addToBackStack(null); // support the Back key
transaction.commit();

Passing values from MainActivity to a Java_Class that extends Fragment fails

To more understand more how fragments work, I created a mainactivity its layout has one<fragment>. and I created two java classes extend fragment and each of these classes has its own layout.
In the java class that extends fragment, I initailise my views textview inside onActivityCreated(), and as shown below in the code in the same class, i created two methods setbtnclicks(int clicks) and getbtnclicks(). From the mainactivity i assign number of clicks of a button to setbtnclicks() and i try to display the number of clicks on the textview of the class that extends fragment by calling getbtnclicks from inside onActivivtyCreated(). But the problem is, this method always displays zero, as if the number of clicks are not incremented.
MainActivtity:
//global variable
private int i = 0;
...
...
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment mSelectedFragment;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
switch (v.getId()) {
case R.id.btn00:
mSelectedFragment = new Fragment00();
mFragmentManager = getFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragment00ID, mSelectedFragment);
mFragmentTransaction.commit();
mFragment00.setBtnClicks(i);
i++;
break;
Java_Class "Fragment00"
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated(): "+order);
order++;
mTv = (TextView) getView().findViewById(R.id.fragment00Tv00);
mTv.setText("the Button was clicked "+getBtnClicks()+ " time(s)");
Log.i(TAG, "onActivityCreated(): "+getBtnClicks());
}
Once i have implemented a more or less same program like yours.The objective was to increment the number of button clicks in both the fragments.Here is the code.Hope it helps:
MainActivity.java
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
public class MainActivity extends Activity implements OnClickListener{
Button btn1,btn2;
int click1=0,click2=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment fragment = null;
if (v == btn1) {
// do stuff for button left
click1++;
Bundle bundle=new Bundle();
String clickstring1=Integer.toString(click1);
bundle.putString("name1",clickstring1 );
fragment = new FragButton1();
fragment.setArguments(bundle);
}
if (v == btn2) {
// do stuff for button right
click2++;
Bundle bundle=new Bundle();
String clickstring2=Integer.toString(click2);
bundle.putString("name2",clickstring2 );
fragment = new FragButton2();
fragment.setArguments(bundle);
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
}
else{
Log.e("MainActivity", "Error in creating fragment");
}
}
}
FragButton1.java
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
#SuppressLint("NewApi")
public class FragButton1 extends Fragment{
TextView tv;
public FragButton1(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragbutton1, container, false);
String strtext=getArguments().getString("name1");
tv=(TextView)rootView.findViewById(R.id.tv_times1);
tv.setText(strtext);
return rootView;
}
}
FragButton2.java
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
#SuppressLint("NewApi")
public class FragButton2 extends Fragment{
TextView tv2;
public FragButton2(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragbutton2, container, false);
tv2=(TextView)rootView.findViewById(R.id.tv_times2);
String strtext=getArguments().getString("name2");
tv2.setText(strtext);
return rootView;
}
}
Here there are 2 buttons and onclick of a button a new fragment will be launched and the fragment will display the number of times a button is clicked in a textview.
increment i before the fragment Transaction
switch (v.getId()) {
case R.id.btn00:
i++;
mSelectedFragment = new Fragment00();
mFragmentManager = getFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragment00ID, mSelectedFragment);
mFragmentTransaction.commit();
mFragment00.setBtnClicks(i);
break;

Categories

Resources