我在使用zxing onActivityResult()的一些问题。
正如你可以在code见我没有正确地在https://$c$c.google.com/p/zxing/wiki/ScanningViaIntent.
现在的问题是我怎么能赶上onActivityResult()的片段,因为我需要在我的Fragmnet而不是在活动这个数据?
包com.example.testing codereading;
进口android.content.Intent;
进口android.content.pm.ActivityInfo;
进口android.content.pm.ResolveInfo;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Button;
公共类MainFragment扩展片段{
私人按钮mButtonXZing;
@覆盖
公共查看onCreateView(LayoutInflater吹气,父母的ViewGroup,捆绑savedInstanceState){
视图V = inflater.inflate(R.layout.fragment_main,父母,假);
mButtonXZing =(按钮)v.findViewById(R.id.button_xzing);
mButtonXZing.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
IntentIntegrator积分=新IntentIntegrator(getActivity());
integrator.initiateScan();
}
});
返回伏;
}
@覆盖
公共无效onActivityResult(INT申请code,INT结果code,意图意图){
的System.out.println(从来没有在这里);
IntentResult scanResult = IntentIntegrator.parseActivityResult(要求code,因此code,意图);
如果(scanResult!= NULL){
//处理扫描结果
}
//否则继续与其他任何code,你需要在方法
}
}
公共类MainActivity扩展FragmentActivity {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
FragmentManager FM = getSupportFragmentManager();
片f = fm.findFragmentById(R.id.fragmentContainer);
如果(F == NULL){
F =新MainFragment();
fm.beginTransaction()
。新增(R.id.fragmentContainer,F)
。承诺();
}
}
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图意图){
super.onActivityResult(要求code,因此code,意图);
的System.out.println(下称code是抓);
}
}
解决方案
如果任何一个有同样的问题,这里是我的解决方案。
包com.example.testing codereading;
进口android.content.Intent;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentManager;
公共类MainActivity扩展FragmentActivity {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_fragment);
FragmentManager FM = getSupportFragmentManager();
片f = fm.findFragmentById(R.id.fragmentContainer);
如果(F == NULL){
F = MainFragment.newInstance(启动应用程序);
。fm.beginTransaction()加(R.id.fragmentContainer,F).commit();
}
}
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,
意向意图){
super.onActivityResult(要求code,因此code,意图);
的System.out.println(下称code是抓);
IntentResult scanResult = IntentIntegrator.parseActivityResult(
请求code,因此code,意图);
//处理扫描结果
如果(scanResult!= NULL){
FragmentManager FM = getSupportFragmentManager();
片段newFrame = MainFragment.newInstance(scanResult.toString());
fm.beginTransaction()代替(R.id.fragmentContainer,newFrame).commit()。
}
}
}
包com.example.testing codereading;
进口android.content.Intent;
进口android.content.pm.ActivityInfo;
进口android.content.pm.ResolveInfo;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.test.suitebuilder.annotation.MediumTest;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Button;
进口android.widget.TextView;
公共类MainFragment扩展片段{
私有静态最后弦乐EXTRA_ code =com.example.testing codereading code。
私人按钮mButtonXZing;
私人TextView的mTextView;
公共静态MainFragment的newInstance(字符串code){
捆绑的args =新包();
args.putSerializable(EXTRA_ code,code);
MainFragment片段=新MainFragment();
fragment.setArguments(参数);
返回片段;
}
@覆盖
公共查看onCreateView(LayoutInflater吹气,父母的ViewGroup,捆绑savedInstanceState){
视图V = inflater.inflate(R.layout.fragment_main,父母,假);
mTextView =(TextView中)v.findViewById(R.id.text_ code);
mTextView.setText((字符串)getArguments()getSerializable(EXTRA_ code));
mButtonXZing =(按钮)v.findViewById(R.id.button_xzing);
mButtonXZing.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
IntentIntegrator积分=新IntentIntegrator(getActivity());
integrator.initiateScan();
}
});
返回伏;
}
@覆盖
公共无效onActivityResult(INT申请code,INT结果code,意图意图){
super.onActivityResult(要求code,因此code,意图);
的System.out.println(从来没有在这里);
IntentResult scanResult = IntentIntegrator.parseActivityResult(要求code,因此code,意图);
如果(scanResult!= NULL){
//处理扫描结果
}
//否则继续与其他任何code,你需要在方法
}
}
I'm having some issue with zxing onActivityResult().
As you can see in the code I did properly invoke new intent as described in https://code.google.com/p/zxing/wiki/ScanningViaIntent.
The question is how can I catch onActivityResult() in Fragment, since I need this data in my Fragmnet and not in Activity?
package com.example.testingcodereading;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainFragment extends Fragment {
private Button mButtonXZing;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_main, parent, false);
mButtonXZing = (Button) v.findViewById(R.id.button_xzing);
mButtonXZing.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.initiateScan();
}
});
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
System.out.println("never here");
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
// handle scan result
}
// else continue with any other code you need in the method
}
}
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment f = fm.findFragmentById(R.id.fragmentContainer);
if(f == null){
f = new MainFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, f)
.commit();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
System.out.println("the code is catch");
}
}
解决方案
If any one have the same issue here is my solution.
package com.example.testingcodereading;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment f = fm.findFragmentById(R.id.fragmentContainer);
if (f == null) {
f = MainFragment.newInstance("Start Application");
fm.beginTransaction().add(R.id.fragmentContainer, f).commit();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
System.out.println("the code is catch");
IntentResult scanResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, intent);
// handle scan result
if (scanResult != null) {
FragmentManager fm = getSupportFragmentManager();
Fragment newFrame = MainFragment.newInstance(scanResult.toString());
fm.beginTransaction().replace(R.id.fragmentContainer, newFrame).commit();
}
}
}
package com.example.testingcodereading;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.test.suitebuilder.annotation.MediumTest;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainFragment extends Fragment {
private static final String EXTRA_CODE = "com.example.testingcodereading.code";
private Button mButtonXZing;
private TextView mTextView;
public static MainFragment newInstance(String code) {
Bundle args = new Bundle();
args.putSerializable(EXTRA_CODE, code);
MainFragment fragment = new MainFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_main, parent, false);
mTextView = (TextView) v.findViewById(R.id.text_code);
mTextView.setText((String) getArguments().getSerializable(EXTRA_CODE));
mButtonXZing = (Button) v.findViewById(R.id.button_xzing);
mButtonXZing.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.initiateScan();
}
});
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
System.out.println("never here");
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
// handle scan result
}
// else continue with any other code you need in the method
}
}