锁定和解锁屏幕后恢复片段
Restore fragment after lock and unlock screen
我有 3(A,B,C) 片段和一个 activity.fragment A 添加到 activity 然后片段 B,C replaced.now 片段 A 替换为片段 B.in 片段 B 我添加了一些细节。然后我在解锁屏幕后锁定屏幕 ..它是打开的 activity 片段 A(已添加)。如何在锁定和解锁屏幕后恢复片段 B
你应该保存扩展 Application
class 的 class 中的状态,因为 activity 将在显示更改后释放(发生锁定屏幕,或方向更改) .
您的新申请 class:
public class myApp extends Application {
public int state; //field that keeps saved state
你的 activity class:
//add this method to save changed state
//then call it every time you change the fragment index
private void onChangeFragment(int stateid) {
myApp sapp = (myApp) this.getApplication();
sapp.state = stateid;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myApp sapp = (myApp) this.getApplication();
//restore fragment from sapp.state value
switch (sapp.state) {
case 0 : //fragment A
{ setContentView(R.layout.fragmentA);
//maybe Fragment newFragment = new MyFragmentA(); ... and so on
break;
}
case 1 : //fragment B
{ setContentView(R.layout.fragmentB);
//maybe Fragment newFragment = new MyFragmentB(); ... and so on
break;
}
}
并在清单中
<application android:icon="@drawable/icon" android:label="@string/app_name"
...android:name=".myApp"`>
其他方法是使用activity之前通过Bundle savedInstanceState
保存的状态。
你的 activity class:
private int state; //field that keeps saved state
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
state = savedInstanceState.getInteger(FRAGMENT_STATE_KEY);
//restore the fragment from state value here
//switch (state) {....
//....
}
// invoked when the activity may be temporarily destroyed, save the instance state here
@Override
public void onSaveInstanceState(Bundle outState) {
out.putInteger(FRAGMENT_STATE_KEY, state);
// call superclass to save any view hierarchy
super.onSaveInstanceState(out);
我有 3(A,B,C) 片段和一个 activity.fragment A 添加到 activity 然后片段 B,C replaced.now 片段 A 替换为片段 B.in 片段 B 我添加了一些细节。然后我在解锁屏幕后锁定屏幕 ..它是打开的 activity 片段 A(已添加)。如何在锁定和解锁屏幕后恢复片段 B
你应该保存扩展 Application
class 的 class 中的状态,因为 activity 将在显示更改后释放(发生锁定屏幕,或方向更改) .
您的新申请 class:
public class myApp extends Application {
public int state; //field that keeps saved state
你的 activity class:
//add this method to save changed state
//then call it every time you change the fragment index
private void onChangeFragment(int stateid) {
myApp sapp = (myApp) this.getApplication();
sapp.state = stateid;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myApp sapp = (myApp) this.getApplication();
//restore fragment from sapp.state value
switch (sapp.state) {
case 0 : //fragment A
{ setContentView(R.layout.fragmentA);
//maybe Fragment newFragment = new MyFragmentA(); ... and so on
break;
}
case 1 : //fragment B
{ setContentView(R.layout.fragmentB);
//maybe Fragment newFragment = new MyFragmentB(); ... and so on
break;
}
}
并在清单中
<application android:icon="@drawable/icon" android:label="@string/app_name"
...android:name=".myApp"`>
其他方法是使用activity之前通过Bundle savedInstanceState
保存的状态。
你的 activity class:
private int state; //field that keeps saved state
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
state = savedInstanceState.getInteger(FRAGMENT_STATE_KEY);
//restore the fragment from state value here
//switch (state) {....
//....
}
// invoked when the activity may be temporarily destroyed, save the instance state here
@Override
public void onSaveInstanceState(Bundle outState) {
out.putInteger(FRAGMENT_STATE_KEY, state);
// call superclass to save any view hierarchy
super.onSaveInstanceState(out);