如何在恢复片段时更改textView的值?

时间:2015-10-06 13:03:46

标签: android android-fragments

我有一项活动,我有一个textViewFramelayout(有片段)和一个按钮。点击按钮时,我正在更改framelayout中的片段和textView中的文本(根据片段)。在按下我的手机的后退按钮时,我能够以某种方式获得具有onBackpressed功能的先前片段,但无法获得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);
}

}

2 个答案:

答案 0 :(得分:0)

转换到第二个片段时,您必须将TextView中的值保存到字段中,然后在导航回来时使用该值调用setText

答案 1 :(得分:0)

我们需要使用findfragmentbyid方法。通过使用此功能,我实现了所需的功能。谢谢