onSavedInstance在Activity和Fragment

时间:2016-06-21 07:02:56

标签: android

我有两个活动,每个活动都有一个片段。活动A托管片段A和活动B托管片段B.我在转换到活动B时从活动A和片段A保存一些状态信息。当按下工具栏后退按钮时,先前保存的信息不可用。在活动A和片段A中,savedInstanceState始终为null。这是我最简单形式的代码。

的AndroidManifest.xml:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity
        android:name=".TestActivity"
        android:label="@string/title_activity_test"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.bigfootgames.android.myapp.MainActivity"/>
    </activity>
</application>

MainActivity.java:

public class MainActivity extends AppCompatActivity
{
  private final String TAG = MainActivity.class.getSimpleName();
  int loc;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    Log.d(TAG, "onCreate - savedInstanceState is " + (savedInstanceState == null ? "null" : "not null"));
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    if (fab != null)
    {
      fab.setOnClickListener(new View.OnClickListener()
      {
        @Override
        public void onClick(View view)
        {
          loc = new Random().nextInt(10);

          Intent intent = new Intent(MainActivity.this, TestActivity.class);
          startActivity(intent);
        }
      });
    }
  }

  @Override
  protected void onSaveInstanceState(Bundle savedInstanceState)
  {
    Log.d(TAG, "onSaveInstanceState - savedInstanceState is " + (savedInstanceState == null ? "null" : "not null"));
    savedInstanceState.putInt("ActivityLocationInt", loc);
    super.onSaveInstanceState(savedInstanceState);
  }

  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState)
  {
    Log.d(TAG, "onRestoreInstanceState - savedInstanceState is " + (savedInstanceState == null ? "null" : "not null"));
    super.onRestoreInstanceState(savedInstanceState);
    loc = savedInstanceState.getInt("ActivityLocationInt");
  }
}

MainActivityFragment.java:

public class MainActivityFragment extends Fragment
{
  private final String TAG = MainActivityFragment.class.getSimpleName();
  int loc;

  public MainActivityFragment()
  {
    loc = new Random().nextInt(10);
  }

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState)
  {
    Log.d(TAG, "onCreate - savedInstanceState is " + (savedInstanceState == null ? "null" : "not null"));
    super.onCreate(savedInstanceState);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  {
    Log.d(TAG, "onCreateView - savedInstanceState is " + (savedInstanceState == null ? "null" : "not null"));
    View v = inflater.inflate(R.layout.fragment_main, container, false);

    return v;
  }

  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState)
  {
    Log.d(TAG, "onActivityCreated - savedInstanceState is " + (savedInstanceState == null ? "null" : "not null"));
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null)
    {
      loc = savedInstanceState.getInt("FragmentLocationInt");
    }
  }

  @Override
  public void onSaveInstanceState(Bundle savedInstanceState)
  {
    Log.d(TAG, "onSaveInstanceState - savedInstanceState is " + (savedInstanceState == null ? "null" : "not null"));
    savedInstanceState.putInt("FragmentLocationInt", loc);
    super.onSaveInstanceState(savedInstanceState);
  }
}

TestActivity.java:

public class TestActivity extends AppCompatActivity
{
  private final String TAG = TestActivity.class.getSimpleName();

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    Log.d(TAG, "onCreate - savedInstanceState is " + (savedInstanceState == null ? "null" : "not null"));
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    if (getSupportActionBar() != null)
    {
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
  }
}

TestActivityFragment.java:

public class TestActivityFragment extends Fragment
{
  private final String TAG = TestActivityFragment.class.getSimpleName();

  public TestActivityFragment()
  {
  }

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState)
  {
    Log.d(TAG, "onCreate - savedInstanceState is " + (savedInstanceState == null ? "null" : "not null"));
    super.onCreate(savedInstanceState);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  {
    Log.d(TAG, "onCreateView - savedInstanceState is " + (savedInstanceState == null ? "null" : "not null"));
    View v = inflater.inflate(R.layout.fragment_test, container, false);

    return v;
  }
}

当我运行程序时,以下是日志消息:

Start application:
    MainActivity:         onCreate - savedInstanceState is null
    MainActivityFragment: onCreate - savedInstanceState is null
    MainActivityFragment: onCreateView - savedInstanceState is null
    MainActivityFragment: onActivityCreated - savedInstanceState is null
Press floating action button to start Activity B:
    TestActivity:         onCreate - savedInstanceState is null
    TestActivityFragment: onCreate - savedInstanceState is null
    TestActivityFragment: onCreateView - savedInstanceState is null
    MainActivity:         onSaveInstanceState - savedInstanceState is not null
    MainActivityFragment: onSaveInstanceState - savedInstanceState is not null
Press toolbar back button to go back to Activity A:
    MainActivity:         onCreate - savedInstanceState is null
    MainActivityFragment: onCreate - savedInstanceState is null
    MainActivityFragment: onCreateView - savedInstanceState is null
    MainActivityFragment: onActivityCreated - savedInstanceState is null

savedInstanceState变量始终为null,除非是onSaveInstanceState覆盖函数调用以保存状态。

这样做的正确方法是什么?我在Manifest文件中尝试过“singleTop”,但这不是我想要的。我想保存并恢复状态信息。

我找不到任何关于使用像这样的Activites和Fragments保存状态的可靠,最新的信息。无论如何,代码不会为一个或另一个保存状态。还有什么我必须要做的吗?

0 个答案:

没有答案