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() 方法实现,那么,两种方法的实现究竟是怎样的呢?让我们来看一看接下来的文章内容. ...
随机推荐
- 【HTML5】选项卡
效果图: HTML: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- 使用FSharp 探索Dotnet图像处理功能2--均衡灰度
重新捡起大学里的图像处理,好像之前什么都没学到,但是我为什么还留着这本书呢?嘿嘿. 看到均衡灰度处理,上来就是积分,概率分布的公式,头微微的有点疼.网上看了点介绍,隔天再拿起书本,总算有了点眉目.简而 ...
- 将 JSP 中数组传递给 js
<% String[] name = { "w ", "a ", "n ", "g"}; % ...
- memcached+tomcat转发forward时 sessionid一直变化的问题
今天遇到了一个很奇怪的问题, 我在tomcat过滤器 中, 对请求过来的静态资源及html页面做了forword转发操作,核心代码如下: private void redirectMobile(Htt ...
- Timer,TimerTask通过程序计数器实现的定时任务
1.程序计数器 程序计数器(Program Counter Register)是一块较小的内存空间,它的作用可以看 做是当前线程所执行的字节码的行号指示器.在虚拟机的概念模型里(仅是概念模型, 各种虚 ...
- 【CSS】如何用css做一个爱心
摘要:HTML的标签都比较简单,入门非常的迅速,但是CSS是一个需要我们深度挖掘的东西,里面的很多样式属性掌握几个常用的便可以实现很好看的效果,下面我便教大家如何用CSS做一个爱心. 前期预备知识: ...
- NOIP2001T4car的旅行计划
洛谷传送门 一看数据就是floyed(毕竟年代久远),然而建图不是那么好贱好建,只知道三个机场,需要判断斜边来求第4个机场坐标. 往后一些麻烦的建图. 最后floyed就好. --代码 #includ ...
- 简单的总结一下iOS面试中会遇到的问题
1.线程是什么?进程是什么?二者有什么区别和联系? 一个程序至少有一个进程,一个进程至少有一个线程: 进程:一个程序的一次运行,在执行过程中拥有独立的内存单元,而多个线程共享一块内存 线程:线程是指 ...
- pl/sql 笔记之存储过程、函数、包、触发器(下)
一.存储过程.存储函数 1.What's This? ①.ORACLE 提供可以把 PL/SQL 程序存储在数据库中,并可以在任何地方来运行它.这样就叫存储过程或函数. ②.存储过程.存储函数的唯 ...
- 【正常向】CODEVS上分黄金
白银上分黄金失败=.= 在之前有很认真的写了一波排序,所以排序并不是很怂,还是那个理,现阶段学习的都是比较简单的排序,都是所谓的冒泡排序啊,桶排序这类,至于插排和选择排序,再往后又是什么快拍就很尬了. ...