After restarting the app, onboarding screen shows again - java

This is code of onboarding fragment 3.in In this code, I have set on click listener to the floating action button fb and tried to save the state of the button clicked.
public class onBoardinFragment3 extends Fragment {
FloatingActionButton fab;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_onboarding3, container,false);
**fab = root.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);**
**SharedPreferences prf = getActivity().getApplicationContext().getSharedPreferences("mypref",MODE_PRIVATE);
SharedPreferences.Editor editor = prf.edit();
editor.putBoolean("isOpened",true);
editor.commit();**
}
});
return root;
}
}
This the code of introductory Activity. The value of
isIntroductoryOpened doe nor become false after clicking the floating
button in onboarding fragment 3.
public class IntroductoryActivity extends AppCompatActivity {
ImageView BgImage;
LottieAnimationView lottieAnimationView;
private static final int Num_Pages = 3;
private ViewPager viewPager;
private ScreenSlidePagerAdapter pagerAdapter;
Animation anim;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
**if (isIntroductoryOpened()){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}**
setContentView(R.layout.activity_introductiory);
BgImage = findViewById(R.id.bgImage);
lottieAnimationView = findViewById(R.id.lottie);
BgImage.animate().translationY(-4000).setDuration(1000).setStartDelay(4000);
lottieAnimationView.animate().translationY(1400).setDuration(1000).setStartDelay(4000);
viewPager = findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
anim = AnimationUtils.loadAnimation(this,R.anim.customanim);
viewPager.startAnimation(anim);
}
**private boolean isIntroductoryOpened() {
SharedPreferences pref = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
Boolean isIntroOpened = pref.getBoolean("isOpened",false);
return isIntroOpened;
}**
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
public ScreenSlidePagerAdapter(#NonNull FragmentManager fm) {
super(fm);
}
#NonNull
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
onBoardinFragment1 tab1 = new onBoardinFragment1();
return tab1;
case 1:
onBoardinFragment2 tab2 = new onBoardinFragment2();
return tab2;
case 2:
onBoardinFragment3 tab3 = new onBoardinFragment3();
return tab3;
}
return null;
}
#Override
public int getCount() {
return Num_Pages;
}
}
}

In the activity before OnBoarding activity, change the code as:
SharedPreferences onBoarding = getSharedPreferences("onBoardingScreen",MODE_PRIVATE);
boolean isFirstTime = onBoarding.getBoolean("firstTime",true);
if(isFirstTime){
SharedPreferences.Editor editor = onBoarding.edit();
editor.putBoolean("firstTime",false);
editor.commit();
Intent intent = new Intent(PrevActivity.this,OnBoarding.class);
startActivity(intent);
}
else{
Intent intent = new Intent(PrevActivity.this,NextActivity.class);
startActivity(intent);
}
PrevActivity is the activity before OnBoarding and NextActivity is the activity after OnBoarding acitivity.
In second line the true value is the default value for firstTime, once getting in the if block, the value will be set to false and the OnBoarding activity will not be shown again to the user.
This code will come in place of the code you have written for going from PrevActivity to the OnBoarding activity.

Related

Switching to first tab in tabbed activity not working properly in android

So I have a weird problem. Basically I have a Main activity with four buttons, each button opens a different tab (there is 4 tabs). Now If I click 4th button I go to the 4th fragment but then if I try clicking on the first tab in tabbed activity it doesn't do anything.
So I can't go to the first tab if I start from 2nd 3rd or 4th unless I click on any other tab first.
It's only the first tab that works like this which makes me think that maybe there's some value that should be changing on menu button click but doesn't?
Here's my TabsActivity onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
//
int defaultValue = 5;
//int page = getIntent().getIntExtra("One", defaultValue);
mViewPager.setCurrentItem(getIntent().getIntExtra("One", defaultValue));
//
//
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager))
Also here's TabsActivity class that takes care of selecting fragments:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
tab1recipes tab1 = new tab1recipes();
return tab1;
case 1:
tab2timer tab2 = new tab2timer();
return tab2;
case 2:
tab3measure tab3 = new tab3measure();
return tab3;
case 3:
tab4substit tab4 = new tab4substit();
return tab4;
default:
return null;
}
}
Here's MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.recipesbtn);
Button btn2 = (Button) findViewById(R.id.timerbtn);
Button btn3 = (Button) findViewById(R.id.measurebtn);
Button btn4 = (Button) findViewById(R.id.subsbtn);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int page=0;
Intent intent = new Intent(MainActivity.this, TabsActivity.class);//startActivity();
intent.putExtra("One", page);
startActivity(intent);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int page=1;
Intent intent = new Intent(MainActivity.this, TabsActivity.class);//startActivity();
intent.putExtra("One", page);
startActivity(intent);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int page=2;
Intent intent = new Intent(MainActivity.this, TabsActivity.class);//startActivity();
intent.putExtra("One", page);
startActivity(intent);
}
});
btn4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int page=3;
Intent intent = new Intent(MainActivity.this, TabsActivity.class);//startActivity();
intent.putExtra("One", page);
startActivity(intent);
}
});
Okay I finally fixed it. Here's how (just in case someone needs it too):
I placed the
mViewPager.setCurrentItem(getIntent().getIntExtra("One", defaultValue));
after
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
and now I feel very stupid

How to know when an activity has finish loaded

I have an activity who load a list of data, this activity its a TabActivity with 3 Tabs, all tabs load a diferent data type.
But the data may be really a bunch of data, for that reason my activity may take a lot of time to show itself.
I start teh activity using
Intent intent = new Intent(MainActivity.this, PcapAnalisys.class);
startActivity(intent);
Then, when the activity is loading shows a black screen, and after a few seconds show the content properly.
There is a way on how to show a message like: "Loading activity... please wait" or something?
This is my code for a my TabActivity:
public class PcapAnalysis extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pcap_analysis);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mViewPager = (ViewPager) findViewById(R.id.container);
tabLayout = (TabLayout) findViewById(R.id.tabs);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//fab.setImageResource(R.drawable.ic_attach_file_white_24dp);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FileChooserDialog dialog = new FileChooserDialog(PcapAnalysis.this);
dialog.show();
dialog.addListener(new FileChooserDialog.OnFileSelectedListener() {
#Override
public void onFileSelected(Dialog source, File file) {
source.hide();
Toast.makeText(source.getContext(),
"File selected: " + file.getAbsolutePath(),
Toast.LENGTH_LONG).show();
Log.i("Archivo seleccionado", file.getAbsolutePath());
new AsyncTask_load(PcapAnalysis.this, file.getAbsolutePath()).execute();
}
public void onFileSelected(Dialog source, File folder, String name) {
source.hide();
Toast.makeText(source.getContext(),
"File created: " + folder.getName() + "/" + name,
Toast.LENGTH_LONG).show();
}
});
}
});
new Task().execute();
}
#Override
public void recreate()
{
super.recreate();
}
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_pcap_analysis, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/*public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pcap_analysis, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}*/
private class Task extends AsyncTask<Void, Integer, Void> {
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
pDialog = new ProgressDialog(PcapAnalysis.this);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setMessage("Procesando...");
pDialog.setCancelable(true);
pDialog.setMax(100);
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... voids) {
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager.setOffscreenPageLimit(3); //cuantas paginas se precargan
mViewPager.setAdapter(mSectionsPagerAdapter);
tabLayout.setupWithViewPager(mViewPager);
//publishProgress(progress);
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
}
#Override
protected void onPostExecute(Void result) {
pDialog.dismiss();
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private Fragment http = new HttpList();
private Fragment dns = new DnsList();
private Fragment ip = new IpList();
private Fragment resume = new ResumeList();
SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
//return PlaceholderFragment.newInstance(position + 1);
switch (position) {
case 0:
return http;
case 1:
return dns;
case 2:
return ip;
case 3:
return resume;
}
return null;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "HTTP";
case 1:
return "DNS";
case 2:
return "RESOURCE IP";
case 3:
return "RESUME";
}
return null;
}
}
}
As you can see this activity use fragments to show the data, and each fragment has a for cicle to load the data (that is in a String array).
Also I use a AsyncTask trying to solve this problem, but is not working. Please help!

Sending an intent between fragments

I will ultimately need to send information from InteractionFragment to DisplayFragment. To my understanding, this needs to go through the container activity first, so I'm trying to send from InteractionFragment to MainActivity.
public class MainActivity extends AppCompatActivity {
Fragment fragmentInteraction, fragmentHistory, fragmentDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FragmentManager fm = getSupportFragmentManager();
fragmentInteraction = fm.findFragmentById(R.id.upper_container);
if (fragmentInteraction == null) {
fragmentInteraction = new InteractionFragment();
fm.beginTransaction()
.add(R.id.upper_container, fragmentInteraction)
.commit();
}
fragmentDisplay = fm.findFragmentById(R.id.lower_container);
if (fragmentDisplay == null) {
fragmentDisplay = new DisplayFragment();
fm.beginTransaction()
.add(R.id.lower_container, fragmentDisplay)
.commit();
}
public void onStart() {
super.onStart();
Intent intent = getIntent();
String intentContents = intent.getStringExtra("editTextString");
}
}
public class InteractionFragment extends Fragment implements View.OnClickListener {
Button buttonScramble, buttonHistory;
EditText mEditText;
Activity context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
// Inflate the layout for this fragment
context = getActivity();
View v = inflater.inflate(R.layout.fragment_interaction, parent, false);
return v;
}
public void onStart() {
super.onStart();
buttonScramble = (Button) getActivity().findViewById(R.id.buttonScramble);
buttonScramble.setOnClickListener(this);
buttonHistory = (Button) getActivity().findViewById(R.id.buttonHistory);
buttonHistory.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonScramble:
//set x to number of letters in word
break;
case R.id.buttonHistory:
//call other fragment
break;
default:
break;
}
//use this to send user input to display fragment
//display fragment will use this info to shuffle letters
mEditText = (EditText) getActivity().findViewById(R.id.edit_text_display);
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("editTextString", mEditText.getText().toString());
startActivity(intent);
}
intentContents returns null and no other solutions I've found for similar problems are working for me. Any suggestions would be greatly appreciated! Thanks!
Did you try using OnFragmentInteractionListener?
This answer: How to implement OnFragmentInteractionListener might help.

Fragment returning null from intent

So i made a bunch of 4 buttons , put an intent to each of them . They all navigate to the same Fragment class . But their extras are different, so if button1 was clicked , Fragment would open and do a certain action , if button2 was clicked , Fragment would do another action and so on. I tried the code on normal activities and it worked , but in fragments its not working . It just returns me "Id is null"
Class sending the intent
public class Intennt extends ActionBarActivity {
Button bt1,bt2,bt3,bt4;
Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intennt);
bt1 = (Button) findViewById(R.id.button);
bt2 = (Button) findViewById(R.id.button2);
bt3 = (Button) findViewById(R.id.button3);
bt4 = (Button) findViewById(R.id.button4);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context, ItemListActivity.class);
i.putExtra(ItemDetailFragment.ID_ACTION,ItemDetailFragment.ACTION_1);
startActivity(i);
}
});
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context, ItemListActivity.class);
i.putExtra(ItemDetailFragment.ID_ACTION,
ItemDetailFragment.ACTION_2);
startActivity(i);
}
});
}
Fragment receiving the intent and extras
public class ItemDetailFragment extends Fragment {
public static final int ACTION_1 = 1;
public static final int ACTION_2 = 2;
public static final int ACTION_3 = 3;
public static final int ACTION_4 = 4;
public static final int ACTION_NULL = -1;
public static final String ID_ACTION = "action_id";
public static final String ARG_ITEM_ID = "item_id";
private DummyContent.DummyItem mItem;
public ItemDetailFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
mItem =
DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
}
int id = getActivity().getIntent().getIntExtra(ID_ACTION, -1);
if (id == ACTION_NULL) {
Log.d("TAG", "id is null");
Toast.makeText(getActivity(), "id is null!",
Toast.LENGTH_SHORT).show();
} else if (id == ACTION_1) {
Log.i("TAG", "ALLOHA! from button 1");
Toast.makeText(getActivity(), "Aloha from button 1!",
Toast.LENGTH_LONG).show();
} else if (id == ACTION_2) {
Log.i("TAG", "Hello from button 2");
Toast.makeText(getActivity(),"Hello from button 2!",
Toast.LENGTH_LONG).show();
}
else if (id == ACTION_3) {
Log.i("TAG", "Hello from button 3");
Toast.makeText(getActivity(),"Hello from button 2!",
Toast.LENGTH_LONG).show();
}
else if (id == ACTION_4) {
Log.i("TAG", "Hello from button 4");
Toast.makeText(getActivity(),"Hello from button 2!",
Toast.LENGTH_LONG).show();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item_detail,
container, false);
return rootView;
}
}
To navigate to a fragment, you need to instantiate the fragment class then use the FragmentManager to proceed to a transaction :
FragmentClass fragment = new FragmentClass();
getFragmentManager()
.beginTransaction()
.replace(R.id.your_fragment_view, fragment)
.commit();
You can proceed to every action right in the current activity (as it stays the main activity).
public class Questions extends DialogFragment {
private static Button Submit;
public int count = 0;
public interface DialogListener {
void onQuestionsFinish(ArrayList<xmlQuestion> l);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_questions, container, false);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Submit = (Button) rootView.findViewById(R.id.SubmitButton);
Submit.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0){
for(int i = 0; i<questionList.size(); i++)
{
questionList.get(i).setAnswer();
}
DialogListener activity = (DialogListener) getActivity();
activity.onQuestionsFinish(questionList);
Questions.this.dismiss();
}
});
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
yInch = metrics.ydpi;
pixHeight = metrics.heightPixels;
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point(); display.getSize(size);
screenWidth=size.x; screenHeight=size.y;
LinearLayout.LayoutParams bodyParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,screenHeight*900/1024);
BodyLayout.setLayoutParams(bodyParams);
WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.height=screenHeight; wmlp.width=screenWidth;
getDialog().getWindow().setAttributes(wmlp);
WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
lp.dimAmount=0.4f;
getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getDialog().setCanceledOnTouchOutside(true);
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_FRAME, android.support.v7.appcompat.R.style.Theme_AppCompat_Light);
}
public Questions()
{
}
}
You don't have to use intents to start fragments. My main benefit for using fragments is being able to reference the data in each one much easier. You can simply create a new instance of your fragment class, and assign it's public variables. Then start it like so...
Questions q = new Questions();
q.count = 0;
q.show(getSupportFragmentManager(), "Dialog Fragment");

Resuming state of fragment activity

I have two ACTIVITY (A and B). I have a FRAGMENT F in ACTIVITY A. My Fragment F cointains 3 EditText (1,2,3).
My app gets a string from Activity B and places it in EditText 3. I have no problem with that. My problem is, when i type something in EditText 1 and 2 then I'll get the string from ACTIVITY B and put it in EditText 3, all of the information I typed in EditText 1 and 2 are gone.
My question is, how will the information I typed in EditText 1 and 2 stay even though I get the string from ACTIVITY B to EditText 3. Here's my code:
ACTIVITY A
public class ActivityA extends FragmentActivity {
ViewPager viewPager = null;
PageIndicator pIndicator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
pIndicator = (PageIndicator)findViewById(R.id.indicator);
pIndicator.setViewPager(viewPager);
}
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter (FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = null;
if (i == 0)
{
fragment = new FragmentF();
}
if (i == 1)
{
fragment = new FragmentG();
}
if (i == 2)
{
fragment = new FragmentH();
}
if (i == 3)
{
fragment = new FragmentI();
}
return fragment;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
if (position == 0)
{
return "TAB.F";
}
if (position == 1)
{
return "TAB.G";
}
if (position == 2)
{
return "TAB.H";
}
if (position == 3)
{
return "TAB.I";
}
return null;
}
}
public void btn_to_actb(View view) {
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
}
ACTIVITY B
public final static String EXTRA_MESSAGE = "com.sample.MESSAGE";
// onClick get button from activity B layout
public void get(View view) {
Intent intent = new Intent(this, ActivityA.class);
TextView textView = (TextView)findViewById(R.id.coordinates);
String message = textView.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
FRAGMENT F
EditText editText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String num = getActivity().getIntent().getStringExtra("com.sample.MESSAGE");
View v = inflater.inflate(R.layout.fragmentf, container, false);
// EditText3 from fragment F layout
editText = (EditText) v.findViewById(R.id.edittext3);
editText.setText(num);
return v;
}
Notice that when you launch your intent to Activity A from B, the Activity A could be recreated o resumed depending on if A was stopped or if it was still on memory.
Your fragment F could be recreated or resumed too depending on how you manage the fragment in your Activity A, so you would lose all the information stored in editTexts.
It would be usefull if you post the code of your Activity A.
The best way to keep the data of fragments between transitions, is store it in your Activity and get it from the Fragment.
In your Fragment
#Override
public void onResume(){
EditText1.setText(getActivity().getTextOfEditText1());
EditText2.setText(getActivity().getTextOfEditText2());
super.onResume();
}
** And in your Activity**
public class A extends Activity{
//...
private String textOfEditText1;
private String textOfEditText2;
//... Other stuff
public String getTextOfEditText1(){
return textOfEditText1;
}
public String getTextOfEditText2(){
return textOfEditText2;
}
}
You should have methods to set the Strings from the Fragment when you modify them.
Hope it helps :)

Categories

Resources