Frage

For the first time im trying to set a landscape orientation on my app. Im having so much problems cause it uses actionbar.tab's and service and i dont know very well yet how all of this will happens on each fragment specifically. The first problem i faced was the asynchrony between activity lifecycle and service, cause the service was unbind when the activity was already recreated. Then i tried to set android:configChanges="orientation" on my manifest to manually handle the events, but the activity stills recreating itself. I dont know how i will handle so many details on orientation changes, the most of data and information i have to retrieve is on service, it guards my app state, but im not getting do it. Please help me before i give up implementing this funcion on my app.

I will post some code.

This is the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.irclient2"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:debuggable="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.irclient2.activity.MainActivity"
        android:configChanges="orientation|keyboardHidden"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name="com.example.irclient2.service.MyService"
        android:exported="false" >
    </service>

    <!-- AS DUAS TAGS SEGUINTES SÃO APENAS PARA O FUNCIONAMENTO DO ADMOB -->

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>

</manifest>

This is my activity:

public class MainActivity extends ActionBarActivity implements
    ServiceConnection {
@Override
protected void onCreate(Bundle savedInstanceState) {
    MyService.log("Activity onCreate()");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout);

    actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowTitleEnabled(false);
    drawerlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerlayout.setDrawerShadow(R.drawable.drawer_shadow, Gravity.LEFT);

    registerReceivers();
    cancelaNotificacao(R.drawable.ic_launcher);

    Intent it = new Intent(this, MyService.class);
    if (!MyService.RUNNING) {
        startService(it);
    }

    if (!bindService(it, this, 0)) {
        mensagemErro("Erro!",
                "Não foi possível conectar ao service. Fechando app.");
        finish();
    }

}

@Override
protected void onDestroy() {
    MyService.log("Activity onDestroy()");
    unregisterReceivers();
    unbindService(this);
    super.onDestroy();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {

    // /////////////////////// CASO PORTRAIT ///////////////////////////

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

    }

    // /////////////////////// CASO LANDSCAPE ///////////////////////////

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

    }

}

@Override
public void onServiceConnected(ComponentName name, IBinder binderservice) {
    MyService.log("Activity onServiceConnected");
    LocalBinder binder = (LocalBinder) binderservice;
    this.service = binder.getService();

    carregaRadio();

    if (MyService.bot != null && MyService.bot.isConnected()) {
        carregaChatdoService();
        selectTab(TITLE_TAB_CHAT);
    } else {
        carregaLogin();
        selectTab(TITLE_TAB_RADIO);
    }

}

@Override
public void onServiceDisconnected(ComponentName name) {
    service = null;

}

//////////// THERE ARE MORE THINGS //////////

My RelativeLayout that is the custom view for the actionbar.tabs:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="64dip"
android:layout_marginLeft="-3dip"
android:layout_marginRight="-3dip"
android:layout_weight="1"
android:orientation="vertical" >

<TextView
    android:id="@+id/tabtitle"
    style="?android:attr/tabWidgetStyle"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:text="Tab Title" />

</RelativeLayout>

Due to my activity always get the data from service, i though it would be easilly with activity being recreated, but i had problems with service connection during the process because service bind and unbind are called and assynchronous. As i already said, is my first time doing it, please tell me what is best thing to do. Thanks in advance.

War es hilfreich?

Lösung

You can use this line of code , in your activity , before the setContentView(R.layout.yourlayout) ;

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) ;

and for the android 3.2+ , you should also add this :

android:configChanges="orientation|screenSize"

And study the documentation here!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top