Fragment回调接口应用间分享数据
package com.example.mydemo; import java.util.List; import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
import android.provider.ContactsContract.CommonDataKinds.Phone; public class MainActivity extends Activity implements
PlaceholderFragment.OnDtaReceivedListener {
String sharedText; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
} Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType(); if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
}
} } void handleSendText(Intent intent) {
sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
} @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_activity_actions, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} @Override
public void onTextSelected(View view) {
// TODO Auto-generated method stub if (sharedText != null) {
// Update UI to reflect text being shared
((TextView) view).setText(sharedText);
} } // @Override
// protected void onActivityResult(int requestCode, int resultCode, Intent
// data) {
// // TODO Auto-generated method stub
// if (requestCode == 1) {
// // Make sure the request was successful
// if (resultCode == RESULT_OK) {
// // Get the URI that points to the selected contact
// Uri contactUri = data.getData();
// // We only need the NUMBER column, because there will be only one row in
// the result
// String[] projection = {Phone.NUMBER};
//
// // Perform the query on the contact to get the NUMBER column
// // We don't need a selection or sort order (there's only one result for
// the given URI)
// // CAUTION: The query() method should be called from a separate thread to
// avoid blocking
// // your app's UI thread. (For simplicity of the sample, this code doesn't
// do that.)
// // Consider using CursorLoader to perform the query.
// Cursor cursor = getContentResolver()
// .query(contactUri, projection, null, null, null);
// cursor.moveToFirst();
//
// // Retrieve the phone number from the NUMBER column
// int column = cursor.getColumnIndex(Phone.NUMBER);
// String number = cursor.getString(column);
// tv.setText(number);
// // Do something with the phone number...
// }
// }
// } }
fragment
package com.example.mydemo; import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; /**
* A placeholder fragment containing a simple view.
*/
public class PlaceholderFragment extends Fragment { OnDtaReceivedListener mCallback; // Container Activity must implement this interface
public interface OnDtaReceivedListener {
public void onTextSelected(View view);
} public PlaceholderFragment() {
} @Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
try {
mCallback = (OnDtaReceivedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnDtaReceivedListener");
}
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false); TextView tv = (TextView) rootView.findViewById(R.id.tv);
mCallback.onTextSelected(tv);
return rootView;
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mydemo"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/CustomActionBarTheme" >
<activity
android:name="com.example.mydemo.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.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" />
</intent-filter>
</activity>
</application> </manifest>
Fragment回调接口应用间分享数据的更多相关文章
- 在controller间分享数据(第一种办法)
Blood 血腥的方法 每个controller都有自己的scope, 同时也可以共享他们老爸的scope内的数据.如果我们想让两个controller共享数据的化, 有多种方法. 最直接血腥的就是在 ...
- PHP--自动回调接口,分批修改数据
/** * 修复 a表 生日格式问题 * @author qin */ public function update_birthday_one() { $this->load->model ...
- fragment间的数据传递
今天我将要讲的是fragment间的数据是如何进行传递的.这里我将举个简单的例子. 首先要有个MainActivity,它托管了MainFragment,而MainFragment又托管了DatePi ...
- contentprovider提供程序间共享数据的统一接口
contentprovider提供程序间共享数据的统一接口
- 黑洞版视频裂变程序【接口版】全新上线,全新UI,支持分享数据统计
黑洞版视频裂变程序[接口版]全新上线,全新UI,支持分享数据统计! 后台效果 程序统一售价:1899/套(包安装,包更新) 注:本程序不属于之前视频程序的更新版,展现形式和广告位设置均不同,是 ...
- 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件
博客地址 : http://blog.csdn.net/shulianghan/article/details/41520569 代码下载 : -- GitHub : https://github.c ...
- 【干货】如何通过OPC自定义接口来实现客户端数据的读取?
上篇博文分享了我的知识库,被好多人关注,受宠若惊.今天我把我在项目中封装的OPC自定义接口的程序分享一下.下面将会简单简单介绍下OPC DA客户端数据访问,以及搭配整个系统的运行环境. OPC(OLE ...
- Android回调接口的写法
方法一: 定义一个接口,里面写想要对外提供的方法,在逻辑层方法的参数里传递进去,让在需要的时候调接口里的方法. 实例一: public class SmsUtils { public interfac ...
- laravel5.5框架中视图间如何共享数据?视图间共享数据的两种方法
laravel框架中视图间共享数据有两种,一种是用视图门面share()方法实现,另一种是用视图门面composer() 方法实现,那么,两种方法的实现究竟是怎样的呢?让我们来看一看接下来的文章内容. ...
随机推荐
- 手机自动化测试:Appium源码分析之跟踪代码分析六
手机自动化测试:Appium源码分析之跟踪代码分析六 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.poptest推出手机自 ...
- 老李分享:《Linux Shell脚本攻略》 要点(三)
老李分享:<Linux Shell脚本攻略> 要点(三) 1.生产任意大小的文件 [root@localhost dd_test]#[root@localhost dd_test]# ...
- BitMap 算法
什么是 BigMap 算法 所谓 BitMap 就是用一个 bit 位来标记某个元素对应的 value,而 key 即是这个元素.由于采用bit为单位来存储数据,因此在可以大大的节省存储空间. 算法思 ...
- AngularJS1.X学习笔记3-内置模板指令
前面学习了数据绑定指令,现在开始学习内置模板指令.看起来有点多,目测比较好理解.OK!开始! 一.ng-repeat 1.基本用法 <!DOCTYPE html> <html lan ...
- matlab实现可调节占空比的方波
我大概讲一下实现的原理:正弦波移相φ,当使得大于sin(φ)的值为1,其他值为-1,占空比就跟这个φ值之间有联系. 占空比原理图如下所示. 结果上图,可以实现调节占空比,方波频率,方波个数. 下面是函 ...
- Git版本管理荟萃
用惯了svn,突然转到git难免有点不适,写个笔记好好备忘总结一番. 一.先看历史(imooc上的一个图): 二.git与svn GIT跟SVN一样有自己的集中式版本库或服务器.但,GIT更倾向于被使 ...
- Linux命令之_Cut(转)
linux之cut用法 cut是一个选取命令,就是将一段数据经过分析,取出我们想要的.一般来说,选取信息通常是针对“行”来进行分析的,并不是整篇信息分析的. (1)其语法格式为:cut [-bn ...
- STM32学习笔记(一)——点亮一个LED
引言 最近报名了2017全国大学生电子设计竞赛,我们学校是第一次参加这个比赛,由于8/9月份就要比赛了,所以现在准备是比较晚的了,指导老师说只能做控制类的题目了,让我们学习一下STM32单片机,51到 ...
- SSE 系列内置函数中的 shuffle 函数
SSE 系列内置函数中的 shuffle 函数 邮箱: quarrying@qq.com 博客: http://www.cnblogs.com/quarryman/ 发布时间: 2017年04月18日 ...
- ArrayList和List主要区别 就是ArrayList类型不安全。
什么是不安全? 1:List大家都知道初始化的时候需要定义其类型,例如 List<int> listTest = new List<int>():而ArrayList,例如:A ...