片段中的意图仅半个月的时间 [英] Intent within fragment works only half the time

查看:124
本文介绍了片段中的意图仅半个月的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过从活动A到FragmentActivity B到片段B.数据的意图,从活动A A按钮开始FragmentActivity B.年份的字符串设置与我在远程服务器上查询mysql数据库过滤器。在code以下的工作,但只有一半的时间。

我不知道是什么原因导致它来过滤只是一些时间,其他时候它只是返回整个表未经过滤的。我猜测它有可能与因为过滤器没有问题的版本的应用程序,而不片段工作,我现在用在片段的意图。它也似乎效率不高,我从送一的活动,意图FragmentActivity B到B.片段的方式我怎样才能解决这个问题呢?

活动答:

 按钮按钮1 =(按钮)findViewById(R.id.button1);    button1.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
           意向意图=新的Intent();
            intent.setClass(FindMovie.this,ShowMovies.class);            intent.putExtra(年,YEAR1);            startActivity(意向);
   }
    });

FragmentActivity乙:

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_show_movies);
    如果(getSupportFragmentManager()。findFragmentByTag(TAG)== NULL){
        最后FragmentTransaction英尺= getSupportFragmentManager()调用BeginTransaction()。
        ft.add(android.R.id.content,新ImageGridFragment(),TAG);
        ft.commit();
    }    意向I = getIntent();    年= i.getStringExtra(年);              //通过另一个意图的B片段重发
    意向意图=新意图(getApplicationContext(),ImageGridFragment.class);
        intent.putExtra(年,一年);
   }

片段B:

  @覆盖
公共查看onCreateView(
        LayoutInflater吹气,ViewGroup中容器,包savedInstanceState){
    最后的视图V = inflater.inflate(R.layout.image_grid_fragment,集装箱,FALSE);
    捆绑额外= getActivity()getIntent()getExtras()。    年= extras.getString(年); 返回伏;
  }


解决方案

我觉得这是一个线程时机的问题,您FragmentActivityB创造片段,被发送到一个线程(我认为),那么你就加入从原来的线程新的价值,有时第二个线程足够快执行FragmentB onCreateView 它获得新的前值。你可以做的是FragmentTransaction过程中添加的值。

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    //创建一个新片段捆绑
    片段片段=新ImageGridFragment();
    束束=新包();    //把变量包和添加到片段
    bundle.putString(年,getIntent()getStringExtra(年));
    fragment.setArguments(包);    //插入片段
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager
            .beginTransaction()
            。新增(android.R.id.content,片段)
            。承诺();
}

在FragmentB获得是这样的:

=一年getArguments()的getString(年);

我认为这将解决这个问题,让我知道什么,虽然发生了,

干杯。

I am trying to pass an intent with data from Activity A to FragmentActivity B to Fragment B. A button from Activity A starts FragmentActivity B. The "year" string sets a filter with which I query a mysql database on a remote server. The code below works but only half the time.

I have no idea what causes it to filter just some times and at other times it just returns the whole table unfiltered. I am guessing it has to be related to the intent I am using in the fragment because the filter worked without issue in the version of the app without the fragment. It also seems inefficient the way I am sending the intent from Activity A to FragmentActivity B to Fragment B. How can I fix this problem?

Activity A:

Button button1 = (Button)findViewById(R.id.button1);

    button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
           Intent intent = new Intent();
            intent.setClass(FindMovie.this, ShowMovies.class);

            intent.putExtra("year", year1);

            startActivity(intent);
   }
    });

FragmentActivity B:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_movies);
    if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
        final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(android.R.id.content, new ImageGridFragment(), TAG);
        ft.commit();
    }



    Intent i = getIntent();

    year = i.getStringExtra("year");

              //resend through another intent to the fragment B
    Intent intent = new Intent(getApplicationContext(), ImageGridFragment.class);


        intent.putExtra("year", year);
   }

Fragment B:

@Override
public View onCreateView(
        LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    final View v = inflater.inflate(R.layout.image_grid_fragment, container, false);


    Bundle extras = getActivity().getIntent().getExtras();

    year = extras.getString("year");

 return v;
  }

解决方案

I think this is a thread timing issue, your FragmentActivityB is creating the fragment, which is sent to a thread (I would think) and then you are adding the new value from the original thread, and sometimes the second thread is fast enough to execute FragmentB onCreateView before it gets the new value. What you can do is add the value of 'year' during the FragmentTransaction.

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Create a new fragment and bundle
    Fragment fragment = new ImageGridFragment();
    Bundle bundle = new Bundle();

    // Put variables in bundle and add to fragment
    bundle.putString("year", getIntent().getStringExtra("year"));
    fragment.setArguments(bundle);

    // Insert the fragment
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager
            .beginTransaction()
            .add(android.R.id.content, fragment)
            .commit();
}

In FragmentB get the 'year' like this:

year = getArguments().getString("year");

I think this would solve the issue, let me know what happens though,

Cheers.

这篇关于片段中的意图仅半个月的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆