测试Fragment(跳转回传onActivityResult问题)

来源:互联网 发布:rds数据库 编辑:程序博客网 时间:2024/06/06 21:38

 activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <FrameLayout        android:id="@+id/fragment_content"        android:layout_width="fill_parent"        android:layout_height="fill_parent"android:layout_weight="9"       >            <LinearLayout        android:id="@+id/ll_control"        android:layout_weight="1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"android:layout_centerHorizontal="true"         >        <Button            android:id="@+id/button1"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="one" />        <Button            android:id="@+id/button2"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="two" />        <Button            android:id="@+id/button3"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="three" />        <Button            android:id="@+id/button4"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="four" />            </LinearLayout></LinearLayout>

activity_one.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#00ffff"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <Button        android:id="@+id/but_fm_one"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_marginLeft="20dp"        android:layout_marginTop="54dp"        android:text="我要跳转" />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/but_fm_one"        android:layout_alignParentBottom="true"        android:layout_marginBottom="96dp"        android:text="我已经跳转了" />    <TextView        android:id="@+id/tv_fm_num"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/textView1"        android:layout_alignBottom="@+id/textView1"        android:layout_toRightOf="@+id/textView1"        android:text="0" />    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/tv_fm_num"        android:layout_alignBottom="@+id/tv_fm_num"        android:layout_toRightOf="@+id/tv_fm_num"        android:text="次" />    <SeekBar        android:id="@+id/seekBar1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/but_fm_one"        android:layout_marginTop="41dp" /></RelativeLayout>

activity_goto.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" >    <TextView        android:id="@+id/textView1"        android:layout_width="150dp"        android:layout_height="100dp"        android:gravity="center"        android:text="跳转成功" /></LinearLayout>

activity_two.xml  就改了一个背景色,就不贴了。




AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.demo_fragment"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="15"        android:targetSdkVersion="19" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.demo_fragment.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>                <activity             android:name="com.example.demo_fragment.SecondActivity"            android:label="@string/app_name"             android:theme="@android:style/Theme.Holo.Light.Dialog.NoActionBar"            >                    </activity>    </application></manifest>



        MainActivity.java

package com.example.demo_fragment;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTransaction;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity  extends FragmentActivity implements OnClickListener{    Button button1;    Button button2;                @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();                            }        private void initView() {        button1 = (Button) findViewById(R.id.button1);        button2 = (Button) findViewById(R.id.button2);                button1.setOnClickListener(this);        button2.setOnClickListener(this);            }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public void onClick(View v) {        if(v == button1){            /**             * 这里要注意:不要把FragmentTransaction transaction作为类的数据成员,而是作为本地变量来使用             *             因为commit不能被同一个FragmentTransaction调用多次,不然就会报异常             *              java.lang.IllegalStateException: commit already called             *              *         在调用commint()之前,你可以用addToBackStack()把事务添加到一个后退栈中,             *         这个后退栈属于所在的activity。有了它,             *         就可以在用户按下返回键时,返回到fragment们执行事务之前的状态。             *              */            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();            transaction.replace(R.id.fragment_content, new OneFragment());            transaction.addToBackStack(null);            transaction.commit();        }else if(v  == button2){            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();            transaction.replace(R.id.fragment_content, new TwoFragment());            transaction.addToBackStack(null);            transaction.commit();                    }            }}<pre name="code" class="java">

OneFragment.java

package com.example.demo_fragment;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.Fragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.webkit.WebView.FindListener;import android.widget.Button;import android.widget.TextView;public class OneFragment extends Fragment {View mView;Button button1;TextView tv_num;private int  count;//跳转次数@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {mView = inflater.inflate(R.layout.activity_one, null);return mView;}@Overridepublic void onResume() {// TODO Auto-generated method stubsuper.onResume();initWidget();Log.i("TAG", "tv_num1--->"+tv_num);button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent =  new Intent(OneFragment.this.getActivity(),SecondActivity.class);OneFragment.this.startActivityForResult(intent,9);}});}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);Log.i("TAG", "onActivityResult");Log.i("TAG", "requestCode:"+requestCode+"    resultCode:"+resultCode);Log.i("TAG", "tv_num2--->"+tv_num);if(requestCode==9 ){count++;Log.i("TAG", "count:"+count);tv_num.setText(""+count);}}private void initWidget() {button1 =  (Button) mView.findViewById(R.id.but_fm_one);tv_num = (TextView) mView.findViewById(R.id.tv_fm_num);}}

TwoFragment.java

package com.example.demo_fragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class TwoFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.activity_two, null);}}



0 0
原创粉丝点击