Android Fragment-APP调用其他APP中的Fragment <1>
一般情况下,APP之间调用对方的界面基本上只限于Activity,以及其他Activity的子类间,通过在AndroidManifest.xml中指定<action android:name ....行为.
今天给一个可以调用对方Fragment的方法.
<1>新建两个android工程fragmentdemo1,fragmentdemo2;
<2>在fragmentdemo1工程中新建两个继承Fragment的类.
<3>在fragmentdemo2添加一个按钮.这个按钮的作用就是调用fragmentdemo1工程中Fragment(实际上还是启动Activity);
<4> : fragmentdemo1工程source :
MainActivity.java
package com.example.fragmentdemo1; import frag.testFragmenttwo; import android.os.Bundle; import android.app.Activity; import android.app.FragmentTransaction; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button mSwitchBtn; private testFragmenttwo ttwofrag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ttwofrag = new testFragmenttwo(); final FragmentTransaction trasection = getFragmentManager() .beginTransaction(); getFragmentManager().beginTransaction(); trasection.replace(R.id.layone, ttwofrag); trasection.addToBackStack(null); trasection.commit(); mSwitchBtn = (Button) findViewById(R.id.button1); mSwitchBtn.setVisibility(View.GONE); mSwitchBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub /* * getFragmentManager().beginTransaction(); * * trasection.replace(R.id.layone, ttwofrag); * trasection.addToBackStack(null); trasection.commit(); */ } }); } @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; } }
xml:这个文件的结果有点特点,我们利用Fragment去占用一个虚位占用的Layout布局ID
<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: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/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="switch fragments" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/layone" android:orientation="horizontal" > <!-- <fragment android:id="@+id/fone" android:name="frag.testFragmentone" android:layout_width="match_parent" android:layout_height="wrap_content" /> --> </LinearLayout> <!-- <fragment android:name="frag.testFragmenttwo" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/ftwo"/> --> " </RelativeLayout>
testFragmentone.java
package frag; import com.example.fragmentdemo1.R; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class testFragmentone extends Fragment{ @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragone, container,false); } }
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" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment one" /> </LinearLayout>
testFragmenttwo.java
package frag; import com.example.fragmentdemo1.R; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class testFragmenttwo extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragtwo, container,false); } }
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" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment two" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
这个工程的配置文件需要修改如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.fragmentdemo1" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.fragmentdemo1.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.FT" /> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> </application> </manifest>
<3> fragmentdemo2工程source:
MainActivity.java
package com.example.fragmentdemo2; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button mStartBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mStartBtn = (Button) findViewById(R.id.button1); mStartBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent in = new Intent("android.intent.action.FT"); in.addCategory("android.intent.category.DEFAULT"); MainActivity.this.startActivity(in); } }); } @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; } }
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: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/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="98dp" android:layout_marginTop="42dp" android:text="startup fragment" /> </RelativeLayout>
这个demo演示一个App的启动另外一App的Activity,但是这个Activity在启动时就"加载Fragmenttwo"的Fragment,所以感觉上App直接启动了另外一个APP的Fragemnt界面.
source code : http://yunpan.cn/QIjvZxSLWE6vg
借鉴了该文章的思路:http://android.9tech.cn/news/2013/1202/39041.html