我对应用程序中的物理菜单按钮有一些奇怪的,不一致的行为。
以下可能有用,也可能没用: 我有一个应用程序,它根据设备+屏幕大小+方向更改其UI。在横向平板电脑上,我使用同时查看2个片段的活动(联系人列表+所选联系人的聊天)。在手机和纵向平板电脑上,我改为使用2个独立的活动;一个用于联系人列表,另一个用于聊天。
我之所以提到这一点是因为我观察到以下奇怪的行为:我的设备上的物理菜单按钮在我的应用程序中在手机和平板电脑纵向方向都没有响应。当平板电脑处于横向时,物理按钮可以按预期工作(显示我的操作栏的溢出菜单)。菜单按钮在我的应用程序之外正常工作。
我原本以为问题出在我手机的Android版本上,但考虑到我也在纵向看到平板电脑的问题,我不知道问题出在哪里可能撒谎。我没有对物理菜单按钮的任何代码进行硬编码,因此不应该为手机和纵向平板电脑的单窗格视图正确复制某些功能。
任何想法或答案将不胜感激! 感谢
编辑:添加了请求的代码。
纵向定位活动:
package com.h.r.android.tcip;
import android.content.ComponentName;
import android.content.ContentUris;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.util.Log;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.h.r.android.tctip.ConversationFragment.Builder;
import com.h.r.android.tctip.data.TacMessage;
import com.h.r.android.tctip.db.AddressmessageSchema;
import com.h.r.android.tctip.settings.PreferencesHelper;
public class ConversationActivity extends LoggedInTCActivity {
@SuppressWarnings("unused")
private final String TAG = ConversationActivity.class.getSimpleName();
/**
* Projection for address database query results.
*/
protected static final String[] ADDRESS_PROJECTION = new String[] {
AddressmessageSchema.UID, AddressmessageSchema.HANDLE };
/**
* The TCService running in the background doing the send/receive work.
*/
protected TCService mService;
/**
* Handler used for the partner handle cursor.
*/
protected final Handler mHandler = new Handler();
/**
* Used to track partner handle changes.
*/
protected ContentObserver mHandleObserver = new ContentObserver(mHandler) {
/*
* (non-Javadoc)
*
* @see android.database.ContentObserver#onChange(boolean)
*/
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
getSupportActionBar().setTitle(getPartnerHandle());
}
};
/**
* The actual partner ID, from the intent.
*/
static long mPartnerId;
/**
* The connection binding us to the Service. We're using this to
* indicate to Android the dependency between this Activity and the running
* Service.
*/
protected final ServiceConnection mConnection = new ServiceConnection() {
/*
* (non-Javadoc)
*
* @see
* android.content.ServiceConnection#onServiceDisconnected(android.content
* .ComponentName)
*/
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
/*
* (non-Javadoc)
*
* @see
* android.content.ServiceConnection#onServiceConnected(android.content
* .ComponentName, android.os.IBinder)
*/
public void onServiceConnected(ComponentName name, IBinder service) {
mService = ((TCService.TCServiceBinder) service)
.getService();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(PreferencesHelper.getThemeId(this));
ConversationFragment frag = new ConversationFragment.Builder(
getIntent().getExtras()).build();
setContentView(R.layout.tc_conv);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/*
if (savedInstanceState != null)
{
mPartnerId = savedInstanceState.getLong("mPartnerId", mPartnerId);
}
else{
*/
// The Fragment's partnerId
mPartnerId = frag.getPartnerId();
//}
getSupportActionBar()
.setTitle(
mPartnerId == 0 ? getString(R.string.tc_all_users_chat)
: getPartnerHandle());
getSupportFragmentManager().beginTransaction()
.add(R.id.conversation, frag).commit();
}
@Override
protected void onResume() {
super.onResume();
startService(new Intent(this, TCService.class));
//startService(new Intent(this, DiscoveryService.class));
// bind to the service to disable notifications while this is up
bindService(new Intent(this, TCService.class), mConnection, 0);
if (mPartnerId != 0) {
Uri handleUri = ContentUris.withAppendedId(TacMessage.ADDRESS_URI,
mPartnerId);
getContentResolver().registerContentObserver(handleUri, true,
mHandleObserver);
mHandleObserver.onChange(true);
}
}
/*
* (non-Javadoc)
*
* @see android.support.v4.app.FragmentActivity#onPause()
*/
@Override
protected void onPause() {
if (mService != null) {
unbindService(mConnection);
}
if (mPartnerId != 0) {
getContentResolver().unregisterContentObserver(mHandleObserver);
}
super.onPause();
}
/**
* Get the partner handle to show in the action bar.
*
* @return
*/
protected String getPartnerHandle() {
// Generic default in case something goes wrong.
String partnerHandle = getString(R.string.t_c_app_name);
Cursor c = null;
try {
String selection = AddressmessageSchema.UID + " = ?";
String[] selectionArguments = { "" + mPartnerId };
c = getContentResolver().query(TacMessage.ADDRESS_URI,
ADDRESS_PROJECTION, selection, selectionArguments, null);
if (c != null && c.moveToFirst()) {
partnerHandle = c.getString(c
.getColumnIndex(AddressmessageSchema.HANDLE));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null) {
c.close();
}
}
return partnerHandle;
}
/*
* Can be called from screen orientation change or keyboard hidden. This is
* being used to prevent the Activity from being destroyed or rebuilt so
* that during zip and image conversion activities, we don't end up killing
* the app by rotating the screen and such.
*
* (non-Javadoc)
*
* @see
* android.support.v4.app.FragmentActivity#onConfigurationChanged(android
* .content.res.Configuration)
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//Builder.CONTACT_UID = getString((int) mPartnerId);
Log.d("gabe", "the config is changing in convo activity" + mPartnerId);
//finish();
}
// Pass in the orientation of sensor so that we keep receiving these
// calls.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
public void onDestroy(Bundle savedInstanceState)
{
Log.d("gabe", "the conver activity is being destroyed" + mPartnerId);
}
public void onSaveInstance(Bundle savedInstanceState)
{
Log.d("gabe", "the con act is c saving instance");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getSupportMenuInflater().inflate(R.menu.chat_only_menu, menu);
return super.onCreateOptionsMenu(menu);
}
/*
* Add the up (ancestral) navigation.
*
* (non-Javadoc)
*
* @see
* com.actionbarsherlock.app.SherlockFragmentActivity#onOptionsItemSelected
* (android.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Ancestral navigation (Home/Up button on Action Bar)
Intent parentActivityIntent = new Intent(this,
TCActivity.class);
// See the ancestral navigation docs about synthesizing a back
// stack, if we ever have need for more back steps than the
// TCActivity class.
// http://developer.android.com/training/implementing-navigation/ancestral.html
parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
// TODO Auto-generated method stub
Fragment fragment = (Fragment) getSupportFragmentManager().findFragmentById(R.id.conversation);
if( null != fragment ) {
fragment.onContextItemSelected(item);
}
return super.onContextItemSelected(item);
}
}
肖像活动的xml,可能不是您想要的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/conversation"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
仅限聊天xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group
android:id="@+id/group_alert_checked"
android:checkableBehavior="all" >
<item
android:id="@+id/menu_alert"
android:checked="false"
android:icon="?attr/buttonface_alert_unchecked"
android:showAsAction="always"
android:title="@string/menu_set_alert_message_mode"/>
</group>
<item
android:id="@+id/menu_attach_existing_picture"
android:icon="?attr/buttonface_existing_picture"
android:showAsAction="always"
android:title="@string/menu_attach_existing_picture">
</item>
<item
android:id="@+id/menu_attach_new_picture"
android:icon="?attr/buttonface_new_picture"
android:showAsAction="always"
android:title="@string/menu_attach_new_picture">
</item>
<item
android:id="@+id/menu_attach_file"
android:icon="?attr/buttonface_attach_file"
android:showAsAction="always"
android:title="@string/menu_attach_file">
</item>
<item
android:icon="?attr/buttonface_overflow"
android:showAsAction="always">
<menu>
<item
android:id="@+id/menu_add"
android:showAsAction="ifRoom"
android:title="@string/menu_new_contact">
</item>
<item
android:id="@+id/menu_clear_conversation"
android:showAsAction="ifRoom"
android:title="@string/menu_clear_conversation">
</item>
<item
android:id="@+id/menu_save_conversation"
android:showAsAction="ifRoom"
android:title="@string/menu_save_conversation">
</item>
<item
android:id="@+id/menu_logout"
android:showAsAction="ifRoom"
android:title="@string/logout"/>
</menu>
</item>
</menu>
我还有片段和片段xml的代码,这对你也很有用,但片段代码非常大。我认识到活动的xml并不是很有用,但没有其余部分。让我知道还有什么可以帮助我,并找出更好的分享方式。
答案 0 :(得分:1)
好的,我认为你需要编辑你的onOptionsItemSelected
覆盖以处理不同的菜单项,这里是一个使用if else而不是switch的例子,使用switches has issues in library projects。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.menu_alert) {
// do something here
return true;
} else if (itemId == R.id.menu_attach_existing_picture) {
// do something here
return true;
} else if (itemId == R.id.menu_attach_new_picture) {
// do something here
return true;
} else if (itemId == R.id.menu_attach_file) {
// do something here
return true;
} else if (itemId == R.id.menu_add) {
// do something here
return true;
} else if (itemId == R.id.menu_clear_conversation) {
// do something here
return true;
} else if (itemId == R.id.menu_save_conversation) {
// do something here
return true;
} else if (itemId == R.id.menu_logout) {
// do something here
return true;
} else if (itemId == android.R.id.home) {
// Ancestral navigation (Home/Up button on Action Bar)
Intent parentActivityIntent = new Intent(this,
TCActivity.class);
// See the ancestral navigation docs about synthesizing a back
// stack, if we ever have need for more back steps than the
// TCActivity class.
// http://developer.android.com/training/implementing-navigation/ancestral.html
parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
finish();
return true;
} else if (super.onOptionsItemSelected(item)) {
// parent activity handled this selection
return true;
}
return false;
}