Sparkplug
Sparkplug

Reputation: 495

Start new activity on orientation change

I want to load a new activity when the orientation changes from potrait to landscape for which I am using:

@Override
public  void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        startActivity(new Intent(this, ABCDActivity.class));
    }

    if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        super.onConfigurationChanged(newConfig);
        finish();
    }
}

But it doesn't change my activity with the change in orientation. Have I missed something?

Upvotes: 0

Views: 2277

Answers (4)

Jelle
Jelle

Reputation: 919

Why would you want to start a new Activity when your orientation changed? It's better to just provide different resources for different configurations! Or use Fragments inside 1 Activity.

More information can be found here:

EDIT: You can just create one activity that recreates itself on orientation change. So remove any configChanges="screenSize..." you have in your AndroidManifest.xml. In the xml files for the activity, you can just link to another Fragment. And again, more information about this can be found in the above links.

MyActivity.java

public class MyActivity extends Activity
{
 @Override
 public void onCreate(Bundle cycle)
 {
    super.onCreate(cycle);
    this.setContentView(R.layout.myactivity);
 }
}

res/layout/myactivity.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment 
        class="com.example.myapp.fragments.TimeFrameGraphFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

And res/layout-land/myactivity.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment 
        class="com.example.myapp.fragments.AllDataGraphFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

Upvotes: 0

user2045814
user2045814

Reputation:

Try this,

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
     super.onConfigurationChanged(newConfig);
     if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
      {
       startActivity(new Intent(this, ABCDActivity.class));
      }
    }

Upvotes: 2

Sunil Kumar
Sunil Kumar

Reputation: 7092

The onConfigurationChanged() method will not be called unless you declared the respective attributes under the in AndroidManifest.xml:

android:configChanges="orientation"

Upvotes: 1

Matt Clark
Matt Clark

Reputation: 28579

In your Manifest Activity tag for each activity you wish to configChanges for, you must place this line:

android:configChanges="orientation|screenSize"

As an app design note... Starting a new activity on orientation change is not advised...

Upvotes: 1

Related Questions