2015-01-05 80 views
0

我想實現下面的代碼的每一件事工作正常,怎麼過 這裏的情況是我面臨的不要,如果Android的後退按鈕被按下開啓第二個活動

  1. mainactivity只是有一個按鈕,打開splashactivity
  2. 飛濺活動做了解析部分然後打開listactvity
  3. 我的工作流程是,如果用戶點擊的主要活動 的按鈕,它會打開飛濺活動和重定向到listactivty出現 問題就撲通活動時,如果用戶點擊在 Andorid的後退按鈕它不回去,不過 解析在splashactivity繼續,用戶會被重定向到 listactivty這種事情不應該發生,當用戶在飛濺 活動已單擊後退按鈕它應該回到mainactivity 並留在MainActivity有本身

如何才能做到這一點

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; 
    } 

} 
+1

解釋你活動流程清晰 – KomalG

+1

你對你的要求很不清楚..請改善你的問題,所以我們可以理解,給你解決 –

+0

我只是想檢查,如果後退按鈕被點擊飛濺活動,然後停止解析那裏本身,而不是去listactivity,這發生即使我按下了飛濺的活動後門按鈕 – 1234567

回答

0

您可以取消解析在MainActivity的onRestart(飼料),因爲這是MainActivity的一個方法,當你按下背部來回將被調用你的解析活動。

你可以閱讀更多關於活動生命週期here

0

爲了檢測後退按鈕按下事件中使用下面的方法

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

如果你想與你的後退按鈕做任何事情,那麼你必須手動覆蓋。

@Override 
Public void onBackPressed() { 

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

return; 
} 
相關問題