1

在我的应用程序中,我有 2 个片段,片段 A 和片段 B。在这两个片段上,我正在做一些 XML 解析和填充列表视图。我想为我的应用程序实现一个启动屏幕。以便它在解析和填充列表视图和完成期间显示当它完成时。为此,我创建了一个活动 SplashActivity。我在 FragmentA 上实现了异步任务,并在预执行部分调用了 SplashActivity。现在,当应用程序启动 SplashActivity 时开始。但我无法在 FragmentA 的后执行时完成这个活动。getActivity().finishActivity() 不起作用。请有人帮我解决这个问题,或者建议我另一种方法来在 Fragment Activity 上实现启动画面。提前致谢....

这是我的片段A=>

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflating layout
        View v = inflater
                .inflate(R.layout.headlines_fragment, container, false);

        return v;

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);
        Log.i("Tag", "onCreateView");

        // We set clear listener
        loading = (ProgressBar) getActivity().findViewById(R.id.progressBar1);

        if (Headlines.headflag == "malayalam") {
            urls = "http://www.abc.com/rssfeeds/19_18_17_25/1/rss.xml";

        }

        if (Headlines.headflag == "english") {
            urls = "http://www.abc.com/en/rssfeeds/1_2_3_5/latest/rss.xml";

        }

        new ProgressAsyncTask().execute();

        MainActivity.refresh.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                new ProgressAsyncTask().execute();

            }
        });

    }

    public void populate_listview() {

        newsList = new ArrayList<HashMap<String, String>>();

        // looping through all song nodes <song>

        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            newsList.add(map);
            MarqueeStr = MarqueeStr + " *** " + Title[i];

        }

    }

    public void StartProgress() {
        new ProgressAsyncTask().execute();
    }

    public class ProgressAsyncTask extends AsyncTask<Void, Integer, Void> {

        int myProgress;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            myProgress = 0;

            Intent intent = new Intent(getActivity(), Splash.class);
            startActivityForResult(intent, 5); //Here I called Splash Activity

            loading.setVisibility(View.VISIBLE);

        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            getActivity().finishActivity(5);// Here I tried to finish flash activity

            if (Title == null) {

                final AlertDialog.Builder alertbox = new AlertDialog.Builder(
                        getActivity());

                alertbox.setMessage("Error in connection.Do you want to retry");
                alertbox.setPositiveButton("retry",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface arg0, int arg1) {
                                // getActivity().finish();
                                // Intent intent = new Intent(getActivity(),
                                // MainActivity.class);
                                // startActivityForResult(intent,1);

                            }

                        });

                alertbox.setNegativeButton("exit",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface arg0, int arg1) {

                                // getActivity().finish();

                            }

                        });

                alertbox.show();

            }

            list = (GridView) getActivity().findViewById(R.id.grid);

            // Getting adapter by passing xml data ArrayList
            adapter = new HeadlinesAdapter(getActivity(), newsList);
            list.setAdapter(adapter);
            loading.setVisibility(View.INVISIBLE);
            Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),
                    "fonts/karthika.TTF");
            MainActivity.flashnews.setText(MarqueeStr);
            if (Headlines.headflag == "malayalam") {
                MainActivity.flashnews.setTypeface(tf);

            }
            list.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    Intent myintent = new Intent(
                            "com.abc.malayalam2headlinespodcast.PODCAST");
                    Bundle mybundle = new Bundle();
                    mybundle.putInt("number", position);
                    myintent.putExtras(mybundle);

                    startActivity(myintent);

                }
            });

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            parse();

            if (Title != null) {
                populate_listview();
            }

            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            // TODO Auto-generated method stub

        }

    }

    public static void parse() {

           //parsing done here
    }

}
4

2 回答 2

4

您不应该将 Splash 实现为一个Activity,而应该作为一个DialogFragment. 只需设置该对话框片段的样式,使其显示为全屏。

关于展示:

SplashDialogFragment splashDlg = new SplashDialogFragment();
splashDlg.show(getSupportFragmentManager(), "SplashScreen");

然后关闭:

SplashDialogFragment splashDlg = (SplashDialogFragment) getSupportFragmentManager().findByTag("SplashScreen");
splashDlg.dismiss();
于 2012-11-30T10:28:44.753 回答
1

只是为了回答您的标题实际上是“从 Fragment 完成一项活动”的问题,根据文档使用界面从 Fragment 到 Activity 进行交互始终是一种好方法:Communicating with Activity from Fragment。因此,必须始终在 Fragment 中声明一个接口,该接口将在您的活动中实现,如下所示:

public class SplashActivity extends Activity implements FragmentA.FragmentListenerCallback {

   public void onFragmentInteraction() {
       //finish your activity or do whatever you like
       finish();
   }       
}

public class FragmentA extends Fragment {
   private FragmentListenerCallback fragmentListenerCallback;

   @Override
   public void onAttach(Activity activity) {
     super.onAttach(activity);
     fragmentListenerCallback = (FragmentListenerCallback) activity;
   }

   public interface FragmentListenerCallback {
       void onFragmentInteraction();
   }

   @Override
   protected void onPostExecute(Void result) {
       // TODO Auto-generated method stub
       super.onPostExecute(result);
       if(fragmentListenerCallback != null){
            fragmentListenerCallback.onFragmentInteraction();
       }
   }
}
于 2017-05-01T06:49:36.950 回答