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() 方法实现,那么,两种方法的实现究竟是怎样的呢?让我们来看一看接下来的文章内容. ...
随机推荐
- 纯JS写最简单的图片轮播
非常简单的一个大图轮播,通过将控制显示位置来进行轮播效果,写来给正在学习的新手朋友们参考交流. 先看效果:(实际效果没有这么快) 先看布局: <div id="display" ...
- Nginx基础学习(一)—Nginx的安装
一.Nginx介绍 1.什么是Nginx? Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.由俄罗斯的程序设计师Igor Sysoev所开 ...
- XMLHttpRequest API 使用指南
一.实例化 XMLHttpRequest 对象 使用 Ajax API 的第一件事情就是实例化 XMLHttpRequest 对象. var xhr = new XMLHttpRequest(); 二 ...
- 怎样配置mysql_installer_community_V5.6........_setup的数据库?
直接访问查看如下路径就可以根据方法匹配 http://jingyan.baidu.com/album/c35dbcb0f1b1448916fcbcc7.html
- SDRAM notebook
/*******************************************************************************/ chapter one * SDR ...
- call和apply的异同
共同点: 作用:调用一个对象的一个方法,以另一个对象替换当前对象.将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象.如果没有提供 thisObj 参数,那么 Global 对 ...
- window下安装mysqldb模块(虚拟环境)
因为在虚拟环境下安装mysql-python走了许多弯路,各种环境问题,特此记录 直接使用pip安装mysql-python会报错 pip install MySQL-python 可以直接选择非虚拟 ...
- 什么是PROFINET IO系统的实时性
实时系统是指系统能及时响应外部事件的请求,在规定的时间内完成对该事件的处理,并控制所有实时任务协调一致的运行. PROFINET IO系统的实时性就是指当有一个外部事件发生时,从输入信号到传输.到控制 ...
- JS模式---发布、订阅模式
发布订阅模式又叫观察者模式,它定义一种一对多的依赖关系, 当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知. document.body.addEventListener('click', ...
- ETL开发面试问题加吐槽加职业发展建议
写在前面: 作为甲方,对于乙方派来的开发人员,我是会自己面一下.总体来说遇到的水平不一,于是经过这三年多的面(cui)试(can),总结了一套自己的面试套路,中间也遇到过很多想吐槽的东西,于是大概记录 ...