У меня есть активность, и у меня есть textView
, Framelayout
(есть фрагменты) и кнопка. нажав на кнопку, я меняю фрагменты на framelayout
и текст в textView
(согласно фрагментам). При нажатии кнопки «Назад» на моем телефоне я как-то могу получить предыдущий фрагмент с функцией onBackpressed
, но не смог получить предыдущее значение на textView
. как это сделать.Как изменить значение textView при возобновлении фрагментов?
public class Quetionare extends AppCompatActivity {
Toolbar toolbar;
TextView tv;
Fragment fr;
Button next;
intro into;
SelectCar selectCar;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quetionare);
initToolbar();
next=(Button)findViewById(R.id.button2);
tv = (TextView)findViewById(R.id.textView5);
fr = new intro();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container, fr);
fragmentTransaction.commit();
}
private void initToolbar()
{
toolbar = (Toolbar)findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
{
actionBar.setDisplayHomeAsUpEnabled(true);
}
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
public void selectFrag(View view)
{
if(view==findViewById(R.id.button2))
{
fr = new SelectCar();
next.setVisibility(View.GONE);
tv.setText("Select Your car model and Plan");
}
else
{
fr = new Checkbox();
tv.setText("Make Your Plan");
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
@Override
public void onBackPressed()
{
if(getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
if (fr == into)
{
next.setVisibility(View.VISIBLE);
}
else
{
next.setVisibility(View.GONE);
}
}
else
super.onBackPressed();
}
@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_quetionare, 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);
}
}
Завершите свой фрагмент с помощью OnBackStackChangedListener, чтобы вы знали, когда фрагмент выскочил из задней части экрана, чтобы принять необходимые меры для фрагментов, которые в настоящее время отображаются пользователю. –