如果按下android后退按钮,请不要打开第二个活动

时间:2015-01-05 12:59:14

标签: android

我正在尝试实现下面的代码,每件事情都可以正常工作 这是我面临的情况

  1. mainactivity只有一个按钮来打开splashactivity
  2. 然后
  3. splash活动会解析部分打开listactvity
  4. 我的工作流程是用户是否点击了主要活动中的按钮 它打开启动活动并重定向到listactivty 如果用户单击,则在启动活动时会出现问题 andorid后退按钮它确实回到主要活动但是 splashactivity中的解析继续,用户被重定向到 listactivty这个东西不应该发生,当用户在启动时 活动点击了后退按钮它应该回到mainactivity 并留在那里
  5. 怎么做呢

    package com.site.name;
    
    public class SplashActivity extends Activity {
    
    
        RSSFeed feed;
        String fileName;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.splash);
    
            fileName = "TDRSSFeed.td";
            Intent i = getIntent();
            int position =  i.getExtras().getInt("position");
            String[] country = i.getStringArrayExtra("country");
    //      //public String RSSFEEDURL = "http://blogname.blogspot.com//feeds/posts/default/-/Awards?alt=rss";
            Toast.makeText(getApplicationContext(), country[position], Toast.LENGTH_SHORT).show();
            //Toast.makeText(getApplicationContext(), country[position], Toast.LENGTH_SHORT).show();
            File feedFile = getBaseContext().getFileStreamPath(fileName);
    
            ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            if (conMgr.getActiveNetworkInfo() == null) {
    
                // No connectivity. Check if feed File exists
                if (!feedFile.exists()) {
    
                    // No connectivity & Feed file doesn't exist: Show alert to exit
                    // & check for connectivity
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage(
                            "Unable to reach server, \nPlease check your connectivity.")
                            .setTitle("TD RSS Reader")
                            .setCancelable(false)
                            .setPositiveButton("Exit",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                int id) {
                                            finish();
                                        }
                                    });
    
                    AlertDialog alert = builder.create();
                    alert.show();
                } else {
    
                    // No connectivty and file exists: Read feed from the File
                    Toast toast = Toast.makeText(this,
                            "No connectivity!",
                            Toast.LENGTH_LONG);
                    toast.show();
                    //feed = ReadFeed(fileName);
                    startLisActivity(feed);
                }
    
            } else {
    
                // Connected - Start parsing
                new AsyncLoadXMLFeed().execute();
    
            }
    
        }
    
        private void startLisActivity(RSSFeed feed) {
    
    
            Bundle bundle = new Bundle();
            bundle.putSerializable("feed", feed);
    
                    // launch List activity
                        Intent intent = new Intent(SplashActivity.this, ListActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
                        // kill this activity
                        finish();
    
        }
    
        private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void> {
    
            @Override
            protected Void doInBackground(Void... params) {
    
                // Obtain feed
                DOMParser myParser = new DOMParser();
                Intent i = getIntent();
                int position =  i.getExtras().getInt("position");
                String[] country = i.getStringArrayExtra("country");
                //feed = myParser.parseXml(RSSFEEDURL);
                //feed = myParser.parseXml("http://blogname.blogspot.com//feeds/posts/default/-/Awards?alt=rss");
                feed = myParser.parseXml("http://blogname.blogspot.com//feeds/posts/default/-/" + country[position] + "?alt=rss");
                if (feed != null && feed.getItemCount() > 0)
                    WriteFeed(feed);
                return null;
    
            }
    
            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
    
                startLisActivity(feed);
            }
    
        }
    
        // Method to write the feed to the File
        private void WriteFeed(RSSFeed data) {
    
            FileOutputStream fOut = null;
            ObjectOutputStream osw = null;
    
            try {
                fOut = openFileOutput(fileName, MODE_PRIVATE);
                osw = new ObjectOutputStream(fOut);
                osw.writeObject(data);
                osw.flush();
            }
    
            catch (Exception e) {
                e.printStackTrace();
            }
    
            finally {
                try {
                    fOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        @Override
        public void onBackPressed() {
            //Include the code here
            return;
        }
    
    }
    

3 个答案:

答案 0 :(得分:0)

您可以取消在MainActivity的onRestart()中解析Feed,因为这是MainActivity的一种方法,当您从解析活动中按回来时将调用该方法。

您可以阅读有关活动生命周期here的更多信息。

答案 1 :(得分:0)

要检测后退按钮按下事件,请使用以下方法

@Override
public void onBackPressed() {
   // do something on back.
   // Change your activity by calling intent
   return;
}

答案 2 :(得分:0)

如果您想使用后退按钮执行任何操作,则必须手动覆盖它。

@Override
Public void onBackPressed() {

//do whatever you want to do as your question is quite confusing.    

return;
}