我在开发Android应用程序时遇到问题,当我尝试在我的片段视图中按下后退按钮时,它似乎接近同一片段的精确副本。如果我再按一次它然后进入我的主要活动(我想在第一次按下按钮时发生)?
这是我调用片段的窗口
public class DaysWindow extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_days_window);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
Scanner scan = new Scanner(message);
scan.useDelimiter(";");
if(savedInstanceState==null){
DaysFragment days = new DaysFragment();
days.setArguments(intent.getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.container, days).commit();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.days_window, menu);
return true;
}
}
这是我看过两次的片段
public class DaysFragment extends android.support.v4.app.Fragment {
String days;
String message;
public View onCreateView(LayoutInflater inflater, ViewGroup Container, Bundle SavedInstanceState){
View view=inflater.inflate(R.layout.fragment_days, Container, false);
days=getArguments().getString(MainActivity.EXTRA_MESSAGE);
TextView textView= (TextView) view.findViewById(R.id.DaysTextView);
Scanner scan = new Scanner(days);
scan.useDelimiter(";");
textView.setText(scan.next());
return view;
}
}