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() 方法实现,那么,两种方法的实现究竟是怎样的呢?让我们来看一看接下来的文章内容. ...
随机推荐
- C# 快速高效率复制对象另一种方式 表达式树
1.需求 在代码中经常会遇到需要把对象复制一遍,或者把属性名相同的值复制一遍. 比如: public class Student { public int Id { get; set; } publi ...
- 将 JSP 中数组传递给 js
<% String[] name = { "w ", "a ", "n ", "g"}; % ...
- JavaScript中数组类型的属性和方法
除了Object,Array类型应该是ECMAScript中最常用的类型了. ECMAScript的数组虽然也是数据的有序列表,但还是与其他语言中的数组有很大的区别.比如ECMAScript数组每一项 ...
- 【Azure】Azure学习方法和学习资料
学习方法: DEX为入门培训,fundamental book进阶材料,Azure 官方为补充权威材料,网站一些大拿的Blog是很多实践精华,推荐阅读. 推荐教材和学习内容: EDX培训:http:/ ...
- reids数据类型
今天第一次开通,写的不好,请谅解 redis并不是简单的key-value存储,实际上它是一个数据结构服务器,支持不同类型的值,也就是说,我们不仅仅把字符串当作键所指向的值, 如下这些数据 ...
- IntelliJ IDEA应用[一]下载与安装
一.IntelliJ IDEA 12.1.6的下载 IntelliJ IDEA的官方下载网站:http://www.jetbrains.com/idea/download/
- Android -- Android下的NDK开发(一)
1,NDK是什么?为什么要进行NDK开发? NDK:Native Development Kit.Android应用运行在Dalvik虚拟机中.NDK允许开发人员使用本地代码语言(例如C和C++)实现 ...
- java复习(5)---接口、继承、多态
Java作为完全面向对象语言,接口.继承和多态是三个非常重要的概念. 1.继承. (1)关键字: extends (2)子类用super()调用父类构造函数,用super().方法 调用父类的成员方法 ...
- this web application instance has been stopped already.
this web application instance has been stopped already. Could not load oracle/sql/converter_xcharset ...
- Java--向数据库添加txt文件中的批量数据
大家可能会遇到这样的问题,在做一个项目时需要操作数据库,需要有大量的数据需要导入到数据库中,这部分数据存到了txt文档中(可以把Word文档中的数据弄到txt文档),总不能每一条数据都复制黏贴到数据库 ...