android fragment version 4 text issue - java

I wrote android code using fragments (Im a newbe) and i want when fragments switched to put some text in the text view. my fragments are making the switch (like intent) but theange text doesn't change. here is the code:
public class Map_Frag extends Fragment {
---interface---
Communicator callback;
TextView tv;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag_map, container, false);
tv = (TextView)v.findViewById(R.id.textView);
return v;
}
#Override
public void onStart() {
super.onStart();
Bundle bundle = getArguments();
if (bundle != null) {
if (bundle.getBoolean("str1"))
changTextToWalla1("str1",bundle.getInt("x"));
}
}
public void changTextToWalla1(String str1, int x) {
switch (x){
case 1:
tv.setText("number one");
break;
case 2:
tv.setText("number two");
break;
}
}
}
/*
if (bundle != null) {
if (bundle.getBoolean("button1"))
changTextToWalla1("Selected", bundle.getInt("num"));
}
}
---control class--
public class Control_Frag extends Fragment {
Communicator callback;
private Button button1;
private Button button2;
#Nullable
#Override
public void onAttach(Context context){
super.onAttach(context);
callback= (Communicator) context;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag_control,container,false);
button1 = (Button)v.findViewById(R.id.button_1);
button2 = (Button)v.findViewById(R.id.button_2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
callback.button1Clicked("button1", 1);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
callback.button2clicked("button2", 2);
}
});
return v;
}
----communicator---
public interface Communicator {
//here i write the functions that im going to use in the project -
//all the functions that move from one fragmant to another
public void button1Clicked (String str1,int x);
public void button2clicked (String str2,int x);
}
-----main---
public class MainActivity extends FragmentActivity implements Communicator{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.small_device) != null) {
Control_Frag cf = new Control_Frag();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().add(R.id.small_device, cf , "cf").addToBackStack("cf").commit();
// In small device it will show only Control_Frag to begin with
}
}
#Override
public void button1Clicked(String str1, int x) {
//When button 1 is pressed - change text to walla 1
// Identifying Map_Frag in Fragment Manager
Map_Frag ma = (Map_Frag) getSupportFragmentManager().findFragmentById(R.id.frag_content);
// TABLET
if( ma != null){
ma.changTextToWalla1(str1 ,x);
}
else {
ma = new Map_Frag();
Bundle bundle = new Bundle();
bundle.putBoolean(str1, true);
bundle.putInt(str1 ,x);
ma.setArguments(bundle);
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.small_device,ma,"sadf").addToBackStack("sadf").commit();
}
}
#Override
public void button2clicked(String str2, int x) {
Map_Frag ma = (Map_Frag)getSupportFragmentManager().findFragmentById(R.id.frag_content);
if (ma != null) {
ma.changTextToWalla1(str2, x);
}else{
ma = new Map_Frag();
Bundle bundle = new Bundle();
bundle.putBoolean(str2, true);
bundle.putInt(str2, x);
ma.setArguments(bundle);
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.small_device,ma,"asd").addToBackStack("asd").commit();
}
}
}
I could use some help to get it done

Why don't you create a separate fragment xml with the design you want and just call the fragment when a user decides to switch between them.
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}

Related

Retain data in Fragment switching

Working on an app that included 3 fragments. I want to retain data in the FragmentA when the user decides to switch back to FragmentA from FragmentB or FragmentC via BottomNavigationView.
My code works great with changing screen orientation, but for some reason it wont retain data on fragment change.
For testing, I've used only one EditText that should retain data from string.
MainActivity
public class MainActivity extends AppCompatActivity implements
BottomNavigationView.OnNavigationItemSelectedListener {
Fragment Frag = new Fragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this); // mOnNavigationItemSelectedListener
if (savedInstanceState != null)
{
Frag = getSupportFragmentManager().getFragment(savedInstanceState, "Frag");
}
else
loadFragment(new FragmentA());
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment fragment = null;
int i = menuItem.getItemId();
if (i == R.id.fragmentA) {
fragment = new FragmentA();
} else if (i == R.id.fragmentB) {
fragment = new FragmentB();
} else if (i == R.id.fragmentC) {
fragment = new FragmentC();
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment)
{
if (fragment != null)
{
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
Frag = fragment;
return true;
}
return false;
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "Frag", Frag);
}
}
FragmentA
public class FragmentA extends Fragment {
EditText editText;
String string;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
{
string = savedInstanceState.getString("Text", "");
}
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// return super.onCreateView(inflater, container, savedInstanceState);
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.fragmentA, container, false);
editText = view.findViewById(R.id.centerText);
editText.setText(string);
return view;
}
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
string = editText.getText().toString();
outState.putString("Text", string);
}
}
I want that editText retains data when user switches back to FragmentA from FragmentB or FragmentC.
I've been struggling with this problem for some time now, trying different methods with no luck.
Either you can keep the references to your fragments, instead of creating new ones every time like you do here:
if (i == R.id.fragmentA) {
fragment = new FragmentA();
} else if (i == R.id.fragmentB) {
fragment = new FragmentB();
} else if (i == R.id.fragmentC) {
fragment = new FragmentC();
}
You could also use a ViewPager which allows you not only to swipe in-between the screens, but to set viewPager.setOffscreenPageLimit(i); which means that fragments will not be recreated unless they're too far off screen.
Here is your mistake
if (i == R.id.fragmentA) {
fragment = new FragmentA();
} else if (i == R.id.fragmentB) {
fragment = new FragmentB();
} else if (i == R.id.fragmentC) {
fragment = new FragmentC();
}
On click you create new fragment instance
Thank you for your replies.
This is what I changed. It is now working although I'm not sure if that's the elegant way to solve this problem. Data now retains in EditText while navigating back to FragmentA.
MainActivity
public class MainActivity extends AppCompatActivity implements
BottomNavigationView.OnNavigationItemSelectedListener {
Fragment Frag = new Fragment();
Fragment FragmentA;
Fragment FragmentB;
Fragment FragmentC;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this); // mOnNavigationItemSelectedListener
FragmentA = new FragmentA();
FragmentB = new FragmentB();
FragmentC = new FragmentC();
if (savedInstanceState != null)
{
Frag = getSupportFragmentManager().getFragment(savedInstanceState, "Frag");
}
else
loadFragment(FragmentA);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment fragment = null;
int i = menuItem.getItemId();
if (i == R.id.fragmentA) {
fragment = FragmentA;
} else if (i == R.id.fragmentB) {
fragment = FragmentB;
} else if (i == R.id.fragmentC) {
fragment = FragmentC;
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment)
{
if (fragment != null)
{
getSupportFragmentManager().beginTransaction().hide(FragmentA).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
getSupportFragmentManager().beginTransaction().show(fragment).commit();
Frag = fragment;
return true;
}
return false;
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "Frag", Frag);
}
}

How to use Button to switch fragments in a ViewPager?

I have 3 main fragments inside viewpager and they have couple of buttons. I want each button to navigate to another fragment and back button to get back to main fragment. I tried to add fragment transaction but it does not work. What am I doing wrong here?
this is one of the main fragments
public class OneFragment extends Fragment implements View.OnClickListener{
public OneFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one, container, false);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.aButton:
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment_one, AFragment.newInstance());
transaction.commit();
break;
}
}
}
this is the fragment I want to navigate
public class AFragment extends Fragment{
public AFragment() {
}
public static AFragment newInstance() {
AFragment fragment = new AFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.a_fragment, container, false);
}
}
First, define a public method in your Activity to switch tabs in the ViewPager, and also define the onBackPressed() override, which will select the first index when the back button is pressed:
public void selectIndex(int newIndex) {
mViewPager.setCurrentItem(newIndex);
}
#Override
public void onBackPressed() {
int currentPosition = mViewPager.getCurrentItem();
if (currentPosition != 0) {
mViewPager.setCurrentItem(0);
} else {
super.onBackPressed();
}
}
Then, modify the click listener in the Fragment in order to call the selectIndex() method:
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.aButton:
//select the index of AFragment:
((MainActivity)getActivity()).selectIndex(1);
break;
}
}

Android: optimizing fragment transitions

So in my app I have a bunch of fragments that the user navigates through. And there are a lot of methods in my activity class that are used for fragment transitions. These are very similar to each other with the only difference being the class of the newFragment. Here is one these methods:
public void onHelpSelected() {
slideInRight();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
HelpFragment newFragment = new HelpFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
slideOutLeft();
}
}, 500);
}
Then, I also have a few classes that describe each fragment. These are also very similar to each other: they differ only by the amount of buttons and by what these buttons do (but most of the buttons are used for fragment transitions). Here is one of these classes:
public class AuthFragment extends Fragment {
Button authRegButton;
Button authHelpButton;
OnHelpButtonListener helpCallback;
OnRegButtonListener regCallback;
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_auth, container, false);
authHelpButton = (Button) view.findViewById(R.id.auth_help_button);
View.OnClickListener authHelpListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
helpCallback.onHelpSelected();
}
};
authHelpButton.setOnClickListener(authHelpListener);
authRegButton = (Button) view.findViewById(R.id.auth_reg_button);
View.OnClickListener authRegListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
regCallback.onRegSelected();
}
};
authRegButton.setOnClickListener(authRegListener);
return view;
}
public interface OnHelpButtonListener {
void onHelpSelected();
}
public interface OnRegButtonListener {
void onRegSelected();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
helpCallback = (OnHelpButtonListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement OnHelpButtonListener");
}
try {
regCallback = (OnRegButtonListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement OnRegButtonListener");
}
}
}
So, I have many pieces of code that are very similar. I bet there is a way to make all this code look much nicer and work more efficiently.
Thus, my question is how do I do that? :)
Thanks!
Here is how I solved this problem, if someone ever needs this :)
So I made an abstract class for my fragments and worked out all the code there:
public abstract class LoginFragment extends Fragment {
MediaPlayer clickPlayer;
int layoutId;
View view;
OnButtonListener buttonCallback;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(layoutId, container, false);
return view;
}
public interface OnButtonListener {
void onButtonSelected(LoginFragment fragment);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
buttonCallback = (OnButtonListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement OnButtonListener");
}
}
public MyButton setUpButton(int buttonId) {
final MyButton button = (MyButton) view.findViewById(buttonId);
View.OnClickListener authHelpListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
clickPlayer = MediaPlayer.create(getContext(), R.raw.click);
clickPlayer.start();
LoginFragment newFragment = determineFragment(button);
buttonCallback.onButtonSelected(newFragment);
}
};
button.setOnClickListener(authHelpListener);
return button;
}
public LoginFragment determineFragment(MyButton button) {
LoginFragment result;
int buttonId = button.getId();
switch (buttonId){
case R.id.auth_help_button:
result = new HelpFragment();
break;
case R.id.agr_cancel_button:case R.id.help_cancel_button:
case R.id.reg_cancel_button:case R.id.reg_cont_cancel_button:
result = new AuthFragment();
break;
case R.id.agr_back_button:case R.id.reg_cont_button:
result = new RegContFragment();
break;
case R.id.reg_cont_agr_button:
result = new AgrFragment();
break;
case R.id.auth_reg_button:
result = new RegFragment();
break;
default:
result = null;
break;
}
return result;
}

getActivity in Fragment is null after change of orientation (single and one pane layout)

I have a problem and the solutions I've found did not work for me (check for null etc.).
I have a simple activity which hosts 2 fragments in landscape mode and 1 framelayout in portrait mode. When I change the orientation of the device and then change it back, the click handler is not working (getActivity() in DetailFragment is null).
Here is my code so far:
//UserListActivity.java
public class UserListActivity extends AppCompatActivity implements UserListFragment.OnUserListSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_list);
// Check whether the activity is using the layout version with
// the fragment_container FrameLayout. If so, we must add the first fragment
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
UserListFragment fragment = new UserListFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fragment, "");
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
}
}
#Override
public void onUserListSelected(User selectedUser) {
UserDetailFragment userDetailFragment = (UserDetailFragment) getSupportFragmentManager().findFragmentById(R.id.user_detail_fragment);
if (userDetailFragment != null) {
// If detail frag is available, we're in two-pane layout...
userDetailFragment.updateSelectedView(selectedUser);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
//create detailFragment
UserDetailFragment newFragment = new UserDetailFragment();
Bundle args = new Bundle();
args.putString(UserDetailFragment.ARG_SELECTED_USER, new Gson().toJson(selectedUser));
newFragment.setArguments(args);
//replace fragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, newFragment, "");
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
}
//UserListFragment.java
public class UserListFragment extends ListFragment implements AdapterView.OnItemClickListener {
private OnUserListSelectedListener mCallback;
private User[] users;
public interface OnUserListSelectedListener {
void onUserListSelected(User selectedUser);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.user_list_fragment, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (users == null) {
getAllUsers();
} else {
setAdapter();
}
getListView().setOnItemClickListener(this);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnUserListSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnUserListSelectedListener");
}
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mCallback.onUserListSelected(users[position]);
}
private void setAdapter() {
setListAdapter(new UserListAdapter(getActivity(), R.layout.user_list_row, users));
}
private void getAllUsers() {
try {
//...
//Async webCall using volley
//...
users = tmp.getUsers().toArray(new User[tmp.getUsers().size()]);
setAdapter();
//...
}
}
//UserDetailFragment.java
public class UserDetailFragment extends Fragment {
final public static String ARG_SELECTED_USER = "selectedUser";
private User mSelectedUser;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
mSelectedUser = new Gson().fromJson(savedInstanceState.getString(ARG_SELECTED_USER), User.class);
}
return inflater.inflate(R.layout.user_detail_fragment, container, false);
}
#Override
public void onStart() {
super.onStart();
Bundle args = getArguments();
if (args != null) {
updateSelectedView(new Gson().fromJson(args.getString(ARG_SELECTED_USER), User.class));
} else {
updateSelectedView(null);
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(ARG_SELECTED_USER, new Gson().toJson(mSelectedUser));
}
public void updateSelectedView(User selectedUser) {
//??? WHY IS GETACTIVITY() AFTER CHANGE OF ORIENTATION NULL ???
if (getActivity() != null) {
TextView textUserName = (TextView) getActivity().findViewById(R.id.textUserName);
TextView textUserEmail = (TextView) getActivity().findViewById(R.id.textUserEmail);
if (selectedUser != null) {
textUserName.setText(selectedUser.getName());
textUserEmail.setText(selectedUser.getEmail());
mSelectedUser = selectedUser;
} else {
textUserName.setText(null);
}
}
}
}
UserDetailFragment userDetailFragment = (UserDetailFragment) getSupportFragmentManager().findFragmentById(R.id.user_detail_fragment);
Creates a new instance of fragment.
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
UserListFragment fragment = new UserListFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fragment, "");
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
}
is reusing the old instance of fragment .
Therefore, use getFragmentByTag() and dont keep the tag empty string (""), while calling .add

How to pass parameter to Fragment from FragmentActivity

I have a FragmentActivity in my android application.
I use a viewPager to insert tabs in the activity. So I have this code. It works.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_clienti);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String datas= extras.getString("Cliente");
if (datas!= null) {
System.out.println("pippo");
}
}
TabAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position); }
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
#Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Scheda Cliente").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Conto Cliente").setTabListener(tabListener));
apriConnessioneDB();
}
This is the class FragmentStatePagerAdapter
public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
//Fragement for Android Tab
return new infoCliente();
case 1:
//Fragment for Ios Tab
return new infoCliente();
case 2:
//Fragment for Windows Tab
return new infoCliente();
}
return null;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position + 1);
}
}
This is the class Fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.view_infocliente, container, false);
if(savedInstanceState!=null){
//String tmp = savedInstanceState.getString("myKey");
//TextView mTabHost = (TextView) findViewById(android.R.id.labelRagioneSociale);
System.out.println("PIPPPPPPPPPPPPPPPPPPPPPP");
}
TextView btn=(TextView) view.findViewById(R.id.labelRagioneSociale5);
btn.setText("pippo");
return view;
}
Now I want to know How I can pass a parameter to infoCliente class.
In the onCreate method of first class I have this:
String datas= extras.getString("Cliente");
I want pass this parameter to fragment class.
Bundle bundle = new Bundle();
bundle.putInt("battery", bat);
MyFragment fragment=new MyFragment();
fragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fragment_container,fragment);
ft.commit();
Then in onCreate() method of the fragment:
Bundle bundle=getArguments(); int mLabel = bundle.getInt("battery",0);
Define an interface in your Fragment, for example
public interface FragmentCallback {
onSomethingHappend(String param);
}
Implement the interface in your Activity.
Then, when you want to call the callback, do something like this:
FragmentCallback callback = (FragmentCallback) getActivity();
callback.onSomethingHappened(string);

Categories

Resources