1

私は、縦向きと横向きの2つの異なるレイアウトを持つアプリケーションを開発しています。

各レイアウトには2つのフラグメントが含まれています。1つのフラグメントには、インターネットからのビデオを再生するビデオビューが含まれています。向きを変えると、ビデオが再開します。

再起動せずにビデオを再生したい。

これが私のコードです:

**This is my main activity.**
------------------------    


public class MainActivity extends Activity {
            VIdeoFragment vfr = null;
            ImageFragment ifr = null;

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main_activity);
            vfr = new VIdeoFragment();
            ifr = new ImageFragment();
           FragmentTransaction ft1 = getFragmentManager().beginTransaction();
           FragmentTransaction ft2 = getFragmentManager().beginTransaction();
           ft1.add(R.id.upper_view, vfr, "Vfr");
           ft2.add(R.id.down_view, ifr, "IFR");

            ft1.commit();
            ft2.commit();
            }
            }

    **--------------------------------
    VIdeoFragment** 



public class VIdeoFragment extends Fragment {
            VideoView vv,oldVV;
            View view=null;
 public static String movieUri =  "http://www.prep-zone.com/androidVideo/College Admission from Prep Zone (HD).mp4";
            @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
                Uri video = Uri.parse(movieUri); 
                vv.setVideoURI(video);
                vv.start();
                    }
                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.video, container, false);

                    vv = (VideoView) view.findViewById(R.id.videoView1);
                    return view; 
                }
            }
    ---------------------------------------------
    ImageFragment 



public class ImageFragment extends Fragment {

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                    return  inflater.inflate(R.layout.image, container,false);  
                }
}
    -------------------------------------------


<?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" >

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/ic_action_search" />

            </LinearLayout>

    ------------------------------


<?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" >

                <VideoView
                    android:id="@+id/videoView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" />

            </LinearLayout>
                -------------------------------------
                portrait main xml
                ----------------



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/LinearLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <LinearLayout
                    android:id="@+id/upper_view"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/down_view"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" >
                </LinearLayout>

            </LinearLayout>

                ------------------------------------
                landscape xml
                ----------------


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/LinearLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal" >

                <LinearLayout
                    android:id="@+id/upper_view"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1">
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/down_view"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1">
                </LinearLayout>

            </LinearLayout>
--------------------------------------------------------


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

        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

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

        ================================================
        please help.Thank you.
4

2 に答える 2

0

MainActivity のマニフェストに、向きの構成変更を入れます。

android:configChanges="orientation"

次に、メイン アクティビティのコードで、向きの変更を処理する必要がある場合は構成の変更をオーバーライドします。

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
}

これにより、アクティビティの再開が回避され、ビデオの再再生が回避されます。

于 2012-09-19T09:53:01.733 に答える
0

Replying for those, who are still coming to this post for the solution:-( for API level 13 and later)

In manifest.xml file for the MainActivity, add the following in tag

android:configChanges="orientation|screenSize"

android:configChanges -lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity remains running and its onConfigurationChanged() method is called,which we can override to do something specific.

The other way is saving Fragment's state.

In code(VideoFragment class as per the problem statement)

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Code to fetch Fragment saved arguments
        setRetainInstance(true);
    }

By default, the retainInstance property of a fragment is false.So, it is not retained, and it is destroyed and recreated on rotation along with the activity that hosts it. Calling setRetainInstance(true) retains the fragment. When a fragment is retained, the fragment is not destroyed with the activity. Instead, it is preserved and passed along intact to the new activity.

When you retain a fragment, you can count on all of its instance variables to keep the same values. When you reach for them, they are simply there.

For detailed information, refer Retaining Fragment during configuration change

于 2014-09-03T05:18:42.077 に答える