我从活动回来时遇到片段重新加载的问题。让我说明我现在是怎么做的。我正在使用支持lib v4和v7,我的主要活动是
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab = actionBar
.newTab()
.setIcon(R.drawable.icon)
.setTabListener(
new TabListener<MyFragment>(this, "myfragment",
MyFragment.class));
actionBar.addTab(tab);
像这里http://developer.android.com/guide/topics/ui/actionbar.html#Tabs一样创建片段 MainActivity的另一部分
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
actionBar标签中使用的片段
import android.support.v4.app.Fragment;
public class MyFragment extends Fragment {
private ListAdapter myListAdapter;
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
最后是SecondActivity
public class SecondActivity extends ActionBarActivity {
private EditText nameET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.done:
if (saveProfile()) {
NavUtils.navigateUpFromSameTask(this);
} else {
return false;
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
清单的一部分
<activity
android:name="com.package.SecondActivity"
android:icon="@drawable/second"
android:label="@string/second"
android:parentActivityName="com.package.MainActivity"
android:theme="@style/Theme.AppCompat.Light" >
<!-- Parent activity meta-data to support API level 7+ -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.package.MainActivity" />
</activity>
当我在SecondActivity中并点击或调用我的动作MainActivity.onCreate时,当我使用android:launchMode =“singleTop”for MainActivity后,它仍然在右侧选项卡但不知道如何刷新。我在stackoverflow上看到了很多主题但到目前为止没有解决方案