方法在Activity中工作但不在片段中工作

时间:2015-12-18 10:26:38

标签: android-fragments android-service android-fragmentactivity

我将我的活动转换为片段,方法network_reinit_activity之前工作正常,但是当我在片段中运行它时,它不会,这是方法:

private final Context c = getActivity();

private void network_reinit_activity() {

    new Thread(new Runnable() {
        @Override
        public void run() {
            Activity mainAct = (Activity)c;
            Intent new_i = new Intent(getActivity(), NavigationActivity.class);
        mainAct.finish();
        Intent srv_i = new Intent(c, RadioStreamService.class);
        srv_i.putExtra("RS_STREAM_URL", RS_STREAM_URL);
        getActivity().stopService(srv_i);
        if(isInForeground)
    //          startActivity(new_i);
        }
    }).run();


}

1 个答案:

答案 0 :(得分:0)

片段未完成,活动完成,请参阅下面的代码

new Thread(new Runnable() {
        @Override
        public void run() {
            Activity mainAct = (Activity)c;
            Intent new_i = new Intent(getActivity(), NavigationActivity.class);
             getActivity().onBackPressed()   // to finish fragment
             Intent srv_i = new Intent(c, RadioStreamService.class);
             srv_i.putExtra("RS_STREAM_URL", RS_STREAM_URL);
             getActivity().stopService(srv_i);
             if(isInForeground)
             //          startActivity(new_i);
             }
    }).run();