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

 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;//跳转次数
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.activity_one, null);
		
		return mView;
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initWidget();
		Log.i("TAG", "tv_num1--->"+tv_num);
		
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent =  new Intent(OneFragment.this.getActivity(),
						SecondActivity.class);
				OneFragment.this.startActivityForResult(intent,9);
			}
		});
	}
	
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.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 {
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		return inflater.inflate(R.layout.activity_two, null);
	}

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值