按下后退按钮时不会清除内容

时间:2015-06-05 15:46:14

标签: android android-fragments back-stack

我有两个屏幕。

屏幕1:

它有两个编辑文本字段。还有一个按钮。当我按下按钮时,它会加载,屏幕2。

屏幕2: 它只有虚拟静态文本。

现在,当我输入一些字段并单击按钮时,它会加载屏幕2.这是所希望的。但是,当我按下后退按钮时,我应该清除编辑文本字段,因为它开始从onCreateView调用生命周期方法。

但是,我发现它只是编辑文本内容。它没有明确案文。调试代码,让我意识到,将断点放在onCreateView的第一行,给出上次输入的值。之后,onCreateView执行,用“”初始化,然后在恢复时,它被替换为最后输入的值。

我想知道这个原因。

的活动:

package com.meditab.testproject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity implements 

BlankFragment.ButtonClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Creating an object of the Fragment Main Class.
    BlankFragment blankFragment = new BlankFragment();

    // Fragment transaction to replace the fragment.
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.frmLayout, blankFragment, "Fragment");
    fragmentTransaction.addToBackStack(null);

    fragmentTransaction.commit();
}


@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_main, 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);
}

@Override
public void onButtonClicked() {

    Fragment fragment = new MainActivityFragment();
    // Fragment transaction to replace the fragment.
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.frmLayout, fragment, "Fragment");
    fragmentTransaction.addToBackStack(null);

    fragmentTransaction.commit();
}

}

片段A:

package com.meditab.testproject;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * Use the {@link FragmentA#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FragmentA extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private ButtonClickListener buttonClickListener;

public FragmentA() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment FragmentA.
 */
// TODO: Rename and change types and number of parameters
public static FragmentA newInstance(String param1, String param2) {
    FragmentA fragment = new FragmentA();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_a, container, false);

    Button btnClick = (Button) view.findViewById(R.id.btnClick);
    btnClick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonClickListener.onButtonClicked();
        }
    });

    return view;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    buttonClickListener = (ButtonClickListener) activity;
}

@Override
public void onDetach() {
    super.onDetach();
}


public interface ButtonClickListener {
    void onButtonClicked();
}

}

片段B:

package com.meditab.testproject;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class FragmentB extends Fragment {

public FragmentB() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_b, container, false);
}
}

1 个答案:

答案 0 :(得分:0)

这是默认行为。 在onDrestroyView之前,片段管理器保存视图层次结构状态。

当转到back时,片段管理器会恢复该状态,并包含编辑文本中的文本。

您可以覆盖public void onViewStateRestored (Bundle savedInstanceState)以了解状态何时恢复。另请注意文档:

  

这是在onActivityCreated(Bundle)之后和onStart()之前调用的。

所以,如果你真的需要清理状态,可以在onStart期间执行此操作。但请记住,一般来说这是一种糟糕的用户体验。