EDIT: Tôi phát hiện ra rằng Activity đang lưu cá thể, nhưng dữ liệu đã lưu Fragments không làm cho nó trở thành Activity savedInstanceState. Tôi đang tiết kiệm thời gian hiện tại trong outState, nhưng nó không làm theo cách của nó tất cả các con đường lên, như các hoạt động không có gì trong SavedInstanceState của nó cho thời gian và trả về 'null' cho thời gian nếu tôi in nó vào logcat .. ..FragmentPagerAdapter không khôi phục các đoạn khi định hướng thay đổi
Tôi đang xây dựng một ứng dụng có bộ đếm giờ và đồng hồ đếm ngược được tích hợp. Hoạt động lưu trữ cơ bản cho bộ tính giờ là một FragmentActivity lưu trữ một FragementPagerAdapter làm tăng hai đoạn trong mã (Tôi không cung cấp id cho các đoạn trong XML vì chúng không được định nghĩa là các đoạn trong .xml). Tất cả mọi thứ hoạt động tốt cho đến khi một thay đổi định hướng và sau đó các hoạt động có vẻ như nó mất liên lạc với các mảnh cũ và chỉ chọn để tạo ra những cái mới. Điều này có nghĩa là bất kỳ đếm ngược hiện tại nào cũng bị mất và bất kỳ lúc nào được chọn cũng bị mất khi thay đổi cấu hình. Tôi sẽ cần giữ số đếm (nếu nó bắt đầu) và bất kỳ số nào hiện được hiển thị ....
Tôi biết rằng Bộ điều hợp phải tự xử lý những thứ này và tôi đang đặt SetRetainInstance (đúng) trong cả OnCreate và OnCreateView.
Tôi đặt móc vào mã Fragment để cho tôi biết bất cứ khi nào saveInstanceState là KHÔNG null vì vậy ít nhất tôi biết những gì đang xảy ra, nhưng có vẻ như trường hợp này luôn là NULL, và nó tạo ra từ đầu ... luôn luôn.
Một số giải pháp khác để tôi ghi đè instantiateItem, nhưng có vẻ như nó chỉ được sử dụng để đặt lại cuộc gọi lại, một số khác thay đổi cài đặt trong Tệp kê khai (cau mày) .... thiết lập đúng ... nhưng rõ ràng, tôi đang làm rối tung điều gì đó trên đường đi. Tôi chỉ đưa ra mã cho FragmentActivity, FragementPagerAdapter và một chút mã Fragment vì tôi không muốn spam bài đăng có mã có thể không phải là vấn đề.
Các FragmentActivity
public class Timer_Main extends FragmentActivity {
ViewPager pager;
Timer_Pager mAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timer_pager);
mAdapter = new Timer_Pager(this, getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.timer_pager_display);
pager.setAdapter(mAdapter);
}
public static String getTitle(Context ctxt, int position) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("pageItem", pager.getCurrentItem());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
}
Các FragementPagerAdapter
public class Timer_Pager extends FragmentPagerAdapter{
Context ctxt = null;
public Timer_Pager(Context ctxt, FragmentManager fm) {
super(fm);
this.ctxt = ctxt;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: {
return(Countdown_Fragment.newInstance());
}
case 1:
return(Countup_Fragment.newInstance());
}
return null;
}
@Override
public String getPageTitle(int position) {
switch (position) {
case 0:
return(String.format(ctxt.getString(R.string.Countdown_label)));
case 1:
return(String.format(ctxt.getString(R.string.Countup_label)));
}
return null;
}
@Override
public int getCount() {
// doing only three pages, right now...one for countdown, one for countup, and one for Tabata.
return 2;
}
private static String makeFragmentName(int viewId, int position)
{
return "android:switcher:" + viewId + ":" + position;
}
}
Các Fragment có mã hơn một chút trong nó, vì vậy tôi sẽ đẩy trong chỉ là những gì nên là cốt lõi của vấn đề ... .if nhiều hơn là cần thiết, tôi sẽ ghép nó vào.
public class Countdown_Fragment extends Fragment implements OnClickListener {
Calendar CountdownTime = Calendar.getInstance();
static AutoResizeTextView tv;
static ImageButton HoursUp;
static ImageButton HoursDown;
static ImageButton MinutesUp;
static ImageButton MinutesDown;
static ImageButton SecondsUp;
static ImageButton SecondsDown;
static ImageButton Reset;
static ImageButton StartPausecount;
static ImageButton Stopcount;
static Boolean Arewecounting = false;
static Boolean Arewecountingdown = false;
static AutoResizeTextView ThreetwooneGO;
static MyCountdownTimer Countdown_Timer_Activity;
ThreetwooneCount Start_countdown;
static Long MillstoPass;
static Countdown_Fragment newInstance() {
Countdown_Fragment frag = new Countdown_Fragment();
return (frag);
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("CurrentTimer", (String) tv.getText());
Log.v("status", "saved fragment state" + tv.getText());
super.onSaveInstanceState(outState);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
setRetainInstance(true);
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setRetainInstance(true);
View result = inflater.inflate(R.layout.countdown_timer_layout,
container, false);
tv = (AutoResizeTextView) result.findViewById(R.id.timer_textview);
tv.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new TimePickerDialog(getActivity(), t, 0, 0, true).show();
}
});
if (savedInstanceState != null) {
Log.v("status", "fragment is NOT empty");
tv.setText(savedInstanceState.getString("CurrentTimer",
tv.toString()));
} else {
Log.v("status", "fragment is empty");
// tv.setText("00:00:00");
}
tv.resizeText();
ThreetwooneGO = (AutoResizeTextView) result
.findViewById(R.id.timer_countdown_text);
HoursUp = (ImageButton) result.findViewById(R.id.hours_up);
HoursDown = (ImageButton) result.findViewById(R.id.hours_down);
MinutesUp = (ImageButton) result.findViewById(R.id.minutes_up);
MinutesDown = (ImageButton) result.findViewById(R.id.minutes_down);
SecondsUp = (ImageButton) result.findViewById(R.id.seconds_up);
SecondsDown = (ImageButton) result.findViewById(R.id.seconds_down);
Reset = (ImageButton) result.findViewById(R.id.reset);
StartPausecount = (ImageButton) result
.findViewById(R.id.startpausecount);
Stopcount = (ImageButton) result.findViewById(R.id.stopcount);
HoursUp.setOnClickListener(this);
HoursDown.setOnClickListener(this);
SecondsUp.setOnClickListener(this);
SecondsDown.setOnClickListener(this);
MinutesUp.setOnClickListener(this);
MinutesDown.setOnClickListener(this);
Reset.setOnClickListener(this);
StartPausecount.setOnClickListener(this);
Stopcount.setOnClickListener(this);
return (result);
}
public void chooseTime(View v) {
new TimePickerDialog(getActivity(), t, 0, 0, true).show();
}
TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
CountdownTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
CountdownTime.set(Calendar.MINUTE, minute);
CountdownTime.set(Calendar.SECOND, 0);
CountdownTime.set(Calendar.MILLISECOND, 0);
updateLabel();
}
};
private void updateLabel() {
String entiretime;
entiretime = String.format("%tT", CountdownTime);
tv.setText(entiretime);
}
Đây là bộ hẹn giờ tôi đang sử dụng cho Coun tdown ....
public class MyCountdownTimer {
private long millisInFuture;
private long countDownInterval;
Countdown_Fragment ctxt;
AutoResizeTextView Timer_edit;
private volatile boolean IsStopped = false;
public MyCountdownTimer(long pMillisInFuture, long pCountDownInterval,
AutoResizeTextView Artv) {
this.millisInFuture = pMillisInFuture;
this.countDownInterval = pCountDownInterval;
Timer_edit = Artv;
}
public void Start() {
final Handler handler = new Handler();
final Runnable counter = new Runnable() {
public void run() {
if (IsStopped == false) {
if (millisInFuture <= 0) {
Countdown_Fragment.done_counting();
} else {
long sec = millisInFuture/1000;
Timer_edit.setText(String.format("%02d:%02d:%02d",
sec/3600, (sec % 3600)/60, (sec % 60)));
millisInFuture -= countDownInterval;
handler.postDelayed(this, countDownInterval);
}
}
}
};
handler.postDelayed(counter, countDownInterval);
}
public void Cancel() {
IsStopped = true;
Timer_edit = null;
Log.v("status", "Main Timer cancelled");
// this.ctxt = null;
}
public long whatisourcount(){
return(millisInFuture);
}
}
Xin chào .. Tôi đang gặp phải sự cố tương tự. Có cách nào để giữ cho thiết lập giữ lại dụ đúng và làm cho nó hoạt động bởi vì nếu không tôi nên viết lại toàn bộ logic của tôi. Cảm ơn trước :) – jaffa
Không thể tìm thấy một cách xung quanh –
http://stackoverflow.com/questions/6976027/reusing-fragments-in-a-fragmentpageradapter/29287415#29287415 –