Reputation: 9225
I have mainactivity
which contains two tabs and has the following code:
public class MainActivity extends Activity {
private class MyTabListener implements ActionBar.TabListener
{
private Fragment mFragment;
private final Activity mActivity;
private final String mFrag;
public MyTabListener( Activity activity, String fragName )
{
mActivity = activity;
mFrag = fragName;
}
@Override
public void onTabReselected( Tab tab, FragmentTransaction ft )
{
// TODO Auto-generated method stub
}
@Override
public void onTabSelected( Tab tab, FragmentTransaction ft )
{
mFragment = Fragment.instantiate( mActivity, mFrag );
ft.add( android.R.id.content, mFragment );
}
@Override
public void onTabUnselected( Tab tab, FragmentTransaction ft )
{
ft.remove( mFragment );
mFragment = null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar ab = getActionBar();
ab.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );
Tab tab = ab.newTab()
.setText( "Current Trip" )
.setTabListener(
new MyTabListener( this, current.class.getName() ) );
ab.addTab( tab );
tab = ab.newTab()
.setText( "Display Result" )
.setTabListener(
new MyTabListener( this, display.class.getName() ) );
ab.addTab( tab );
File folder = new File(Environment.getExternalStorageDirectory() + "/tc");
boolean success = true;
if (!folder.exists()) {
//Toast.makeText(MainActivity.this, "Directory Does Not Exist, Create It", Toast.LENGTH_SHORT).show();
success = folder.mkdir();
}
if (success) {
//Toast.makeText(MainActivity.this, "Directory Created", Toast.LENGTH_SHORT).show();
} else {
//Toast.makeText(MainActivity.this, "Failed - Error", Toast.LENGTH_SHORT).show();
}
}
}
How do I pass data of a edittext from current.class
to be shown and also used in display.class
for calculation?
Upvotes: 0
Views: 1130
Reputation: 3489
I suggest you use shared prefs e.g.
this is the frag that "writes":
private SharedPreferences prefs; // shared preferences
prefs = getActivity().getSharedPreferences("spa", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("someId", "something"); //or you can use putInt, putBoolean ...
editor.commit();
this is the frag the "reads"
prefs = getActivity().getSharedPreferences("spa", Context.MODE_PRIVATE);
String someId=prefs.getString("someId",someId);
Alternatively you could call the a method in one fragment from the other (it is best to do this via the mainactivity rather than directly).
from frag 1:
((activity)getActivity()).somemethod();
in activity:
fragment2 fragment = (fragment2) getSupportFragmentManager().findFragmentByTag("fragment2");
fragment.somemethod();
Upvotes: 1