当用户从MainActivity
活动回来时,我正在尝试更改Map
中操作栏的图标,但我遇到的问题是onCreateOptionsMenu()
和onPrepareOptionsMenu()
当我从地图活动回到MainActivity时,不再被调用。如何在MainActivity中访问退出的acitionbar?
我尝试以下面的方式访问但没有成功。
我提供任何帮助。
地图活动:
public class Map extends ActionBarActivity{
boolean serviceStatus;
@Override
public void onBackPressed() {
super.onBackPressed();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("sharedServiceStatus", serviceStatus);
editor.commit();
}
}
MainActivity
public class MainActivity extends ActionBarActivity{
boolean serviceStatus = true;
createCheckboxList(final ArrayList<Integer> items){
Intent intent = new Intent(MainActivity.this,
GetLLRD.class);
intent.putExtra("json_data", json);
// getBroadcast; getService
PendingIntent pendingIntent = PendingIntent.getService(
getApplicationContext(), 123, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), 15 * 1000,
pendingIntent);
// cal.getTimeInMillis(); System.currentTimeMillis()
GetLLRD.shouldContinue = true;
} startService(intent);
@Override
protected void onStart() {
super.onStart();
final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
serviceStatus = (mSharedPreference.getBoolean("sharedServiceStatus", true));
ActionBar actionBar = getSupportActionBar();
if(serviceStatus){
actionBar.setIcon(R.drawable.on);
}else{
actionBar.setIcon(R.drawable.off);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
System.out.println("ABC MainActivity onCreateOptionsMenu was invoked.");
MenuItem item = menu.findItem(R.id.menu_toggle);
if(serviceStatus){
item.setIcon(R.drawable.on);
}else{
item.setIcon(R.drawable.off);
}
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
System.out.println("ABC MainActivity onPrepareOptionsMenu was invoked.");
return super.onPrepareOptionsMenu(menu);
}
}
GETLLRD IntentSevice类
public class GetLLRD extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
Intent intent1 = new Intent(
GetLLRD.this, Map.class);
intent1.putExtra("list_data",
data);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent1);
}
}
答案 0 :(得分:0)
如果是这种情况那么你需要一个计数器或一个布尔值来跟踪你来自哪个父活动
从地图活动切换到主要活动时,将额外添加到用作标记的意图
Intent i=new Intent(Map.this, MainActivity.class)
i.putExtra("parent","map");
startActivity(i);
并在MainActivity中
布尔值USE_DEFAULT_ICON = true;
Intent i=getIntent();
if(i.getBundle!=null) {
if("map".equals(i.getBundle.getExtra("parent").toString())){
USE_DEFAULT_ICON ==false;}
和onCreate
ActionBar actionBar = getSupportActionBar();
if(USE_DEFAULT_ICON){
actionBar.setIcon(R.drawable.on);
}else{
actionBar.setIcon(R.drawable.off);
}
另外添加此内容
@Override
protected void onStart() { super.onStart(); invalidateOptionsMenu(); }
答案 1 :(得分:0)
在MainActivity的onResume中尝试一下。因此,当你从MapActivity返回时,将调用onResume,因此下面的代码将起作用。
@Override
protected void onResume() {
super.onResume();
if(serviceStatus){
actionBar.setIcon(R.drawable.on);
}else{
actionBar.setIcon(R.drawable.off);
}
}