正常情况下,我正在尝试将我的位置名称存储在一个包上并在热启动时使用它:
public class LocFragment extends Fragment {
private static final String KEY_LOCATION_NAME = "location_name";
public String location;//="No location name found";
//to be called by the MainActivity as:
//@Override
//public Fragment getItem(int pos) {
//switch (pos) {
// case 0:
// return new LocFragment();
// case 2:....
public LocFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Retrieve location and camera position from saved instance state.
if (savedInstanceState != null) {
location = savedInstanceState.getCharSequence(KEY_LOCATION_NAME).toString();
// Snackbar.make(getView(), location, Snackbar.LENGTH_LONG).show();
System.out.println("location "+location);
//mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
}else{
System.out.println("NULL SI");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
....
List<Address> addresses = null;
Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault());
try {
addresses = geocoder.getFromLocation(
latlang.Lat,
latlang.Lang,
1);
} catch (IOException ioException) {
// Catch network or other I/O problems.
errorMessage = getString(R.string.service_not_available);
// Log.e(TAG, errorMessage, ioException);
Snackbar.make(getView(), errorMessage, Snackbar.LENGTH_LONG).show();
} catch (IllegalArgumentException illegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = getString(R.string.invalid_lat_long_used);
Snackbar.make(getView(),
"Illegal Latitude = " + latlang.Lat + ", Longitude = " +
latlang.Lang, Snackbar.LENGTH_LONG).show();
}
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
System.out.println("Adress Empty No Adress Found");// Snackbar.LENGTH_LONG).show();
location = "Restart the app";
}
} else {
location = addresses.get(0).getLocality();
System.out.println("AM I GETTING Location"+location);
}
@Override
public void onSaveInstanceState(Bundle outState) {
if (location != null) {
outState.putCharSequence(KEY_LOCATION_NAME, location);
super.onSaveInstanceState(outState);
}
}
现在,我希望这种情况:
"NULL SI"
。在此步骤中,当它获得位置名称时,该名称将添加到"KEY_LOCATION_NAME"
上的onSaveInstanceState
但这不是我得到的。
从日志开始,冷启动:
I/System.out: NULL SI
I/System.out: Adress Empty No Adress Found
它在此步骤中获取地址,然后: @ 1st热门开始
I/System.out: NULL SI
I/System.out: AM I GETTING LocationMountain View
@ 2nd Hot start (只是为了确认,即使知道位置也会显示相同的内容):
I/System.out: NULL SI
I/System.out: AM I GETTING LocationMountain View
我在这里缺少什么?
更新
只需检查代码调用的onSaveInstanceState
实际上是从不。可能这是原因吗?我怎么称呼这个?
更新:MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
private DrawerLayout drawer;
private ListView mDrawerList;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawerLayout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
final ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
/*
NavigationView
*/
ImageButton leftNav = (ImageButton) findViewById(R.id.left_nav);
ImageButton rightNav = (ImageButton) findViewById(R.id.right_nav);
leftNav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tab = pager.getCurrentItem();
if (tab > 0) {
tab--;
pager.setCurrentItem(tab);
} else if (tab == 0) {
pager.setCurrentItem(tab);
}
}
});
rightNav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tab = pager.getCurrentItem();
tab++;
pager.setCurrentItem(tab);
}
});
}
public void showDateDialog() {
DialogFragment CalFragment = new CalFragment();
CalFragment.show(getSupportFragmentManager(), "timePicker");
}
/* @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
// int id = menu.getItem(int );
// if (id = )
return true;
}*/
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
//invalidateOptionsMenu();
return new LocFragment();
case 2:
return ThirdFragment.newInstance("ThirdFragment, instance 1");
case 1:
return SecondFragment.newInstance();
case 3:
return FirstFragment.newInstance("FirstFragment, Instance 1");
default:
return ThirdFragment.newInstance("ThirdFragment, default");
}
}
@Override
public int getCount() {
return 4;
}
}