我知道我在做這在本質上錯了,但我想我會問反正。我似乎沒有找到嚴格的答案,並且我正在做這樣的問題的一切答案。所以,我認爲它與碎片或其他東西有關。我對Android開發相當陌生,請耐心等待。如何強制一個活動從頭開始?
所以,我有一個畫廊視圖。當您點擊這個圖庫中的其中一個項目時,它會啓動一個活動的目的,該目標會在尋呼機中顯示該項目以及其周圍的其他項目。當您單擊回家的操作欄上,或者點擊後退按鈕,然後單擊做同樣的事情不同的畫廊看,新的負載罰款。
當你回家時,轉到另一個應用程序,並長按主頁按鈕,並打我的應用程序,它加載正常(儘管,並不總是在正確的地方,我認爲是我需要的東西添加到savedInstanceState,但我離題..)
然而,當我打的主頁按鈕。點擊啓動器圖標(啓動mainActivity),然後點擊其中一個畫廊,它會顯示活動中的舊圖標。這就是問題所在,如果我能夠在保持所有工作功能的同時從頭開始,則可以糾正這一問題。
在XML,該活動被指定爲這樣:
<activity
android:name=".NewsViewCatActivity"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden|screenSize|screenLayout|uiMode"
android:theme="@style/Theme.BgsuNews" >
<intent-filter>
<action android:name="edu.bgsu.news.VIEW_CAT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
我打電話的意圖這樣:
// Launch a News View Activity
Intent myIntent = new Intent(context,
NewsViewCatActivity.class);
myIntent.putExtra("news", (Serializable)subcat);
myIntent.putExtra("title", catTitle);
myIntent.putExtra("pos", position);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(myIntent);
和活動調用這個新的活動/意圖被定義爲如下:
<activity
android:name=".NewsActivity"
android:clearTaskOnLaunch="true"
android:configChanges="orientation|keyboardHidden|screenSize|screenLayout|uiMode"
android:label="@string/shortcut_name"
android:launchMode="singleInstance"
android:theme="@style/Theme.BgsuNews" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我的活動本身使用下面的代碼:
package edu.bgsu.news;
import java.io.Serializable;
import java.util.List;
import com.viewpagerindicator.CirclePageIndicator;
import edu.bgsu.news.api.NewsItem;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.Menu;
import android.support.v4.view.MenuItem;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MenuInflater;
import android.view.Window;
public class NewsViewCatActivity extends FragmentActivity
{
private static final String TAG = "BGSUNEWS";
private ViewPager mViewPager;
private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
private CirclePageIndicator mCircleIndicator;
private List<NewsItem> news;
private String title;
private Integer pos;
private String catID;
private ActionBar actionBar;
public void onNewIntent(Intent intent)
{
Log.i(TAG, "onNewIntent for NewsCatActivity");
setIntent(intent);
loadIt();
}
@SuppressWarnings("unchecked")
private void loadIt()
{
news = null;
Bundle extras = getIntent().getExtras();
news = (List<NewsItem>) extras.get("news");
title = (String) extras.get("title");
pos = (Integer) extras.get("pos");
catID = (String) extras.get("catID");
if (mMyFragmentPagerAdapter != null)
mMyFragmentPagerAdapter.notifyDataSetChanged();
mViewPager.setCurrentItem(pos);
actionBar.setTitle(title);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.news_view_menu, menu);
NewsItem cNews = news.get(mViewPager.getCurrentItem());
changeMenuItem(menu.findItem(R.id.news_fave), cNews.isFaved());
return true;
}
public void onSaveInstanceState(Bundle savedInstanceState)
{
savedInstanceState.putSerializable("news", (Serializable) this.news);
}
@SuppressWarnings("unchecked")
public void onRestoreInstanceState(Bundle savedInstanceState)
{
this.news = (List<NewsItem>) savedInstanceState.getSerializable("news");
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
NewsItem cNews;
switch (item.getItemId())
{
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, NewsActivity.class);
intent.putExtra("catID", catID);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
return true;
case R.id.news_share:
// Uri uri = Uri.parse(news.getLink());
cNews = news.get(mViewPager.getCurrentItem());
Intent sharingIntent = new Intent(
android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,
cNews.getName() + " - " + cNews.getLink());
sharingIntent.putExtra(android.content.Intent.EXTRA_TITLE,
cNews.getName());
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Shared: " + cNews.getName());
startActivity(Intent.createChooser(sharingIntent, "Share "
+ cNews.getName()));
return true;
case R.id.news_fave:
cNews = news.get(mViewPager.getCurrentItem());
if (cNews.isFaved())
{
cNews.unFave();
Log.i(TAG, "Unfaved it!");
changeMenuItem(item, false);
} else
{
cNews.fave();
Log.i(TAG, "Faved it!");
changeMenuItem(item, true);
}
return true;
case R.id.news_open:
Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(news
.get(mViewPager.getCurrentItem()).getLink()));
startActivity(intent1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void changeMenuItem(MenuItem item, boolean isFaved)
{
if (isFaved)
{
item.setIcon(R.drawable.ic_action_star_hl);
item.setTitle(R.string.news_unfave);
} else
{
item.setIcon(R.drawable.ic_action_star);
item.setTitle(R.string.news_fave);
}
}
public void onResume()
{
Log.i(TAG, "onResume for NewsCatActivity");
runViewStuffs();
loadIt();
super.onResume();
}
public void onCreate(Bundle savedInstanceState)
{
Log.i(TAG, "onCreate for NewsCatActivity");
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_multiple_news);
// onResume called
}
public void runViewStuffs()
{
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
mViewPager = (ViewPager) findViewById(R.id.news_pager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(
getSupportFragmentManager());
mViewPager.setAdapter(mMyFragmentPagerAdapter);
mCircleIndicator = (CirclePageIndicator) findViewById(R.id.news_indicator);
mCircleIndicator.setViewPager(mViewPager);
mCircleIndicator.setOnPageChangeListener(new MyPagerListener());
}
public class MyPagerListener implements ViewPager.OnPageChangeListener
{
@Override
public void onPageSelected(int position)
{
// TODO Grab the menu item and change it.
invalidateOptionsMenu();
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels)
{
}
@Override
public void onPageScrollStateChanged(int state)
{
}
}
private class MyFragmentPagerAdapter extends FragmentPagerAdapter
{
public MyFragmentPagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int index)
{
return NewsViewFragment.newInstance(news.get(index),
getApplicationContext());
}
@Override
public int getCount()
{
return news.size();
}
public int getItemPosition(Object obj)
{
return news.indexOf(obj);
}
}
}
我設置新的意圖,並呼籲所有的事情我通常叫,所以順便我想它應該「只是工作」(TM)。但是,它不是。
所以我一般的問題是:如何強制該活動重新開始。
和我的另類問題是:我在做什麼這是本質上錯了嗎?
我根據一般的慰問和藉口任何可能使用的onResume(被視爲「n00by」
是不是有一個特別的原因,你爲什麼在你的''中使用'android:launchMode =「singleTop」'(可能會導致你的問題)和龐大的'android:configChanges'(這只是可憐的形式)元件? –
CommonsWare
2012-02-03 18:40:13
在這個問題的範圍內,配置更改並不重要。雖然我使用singleTop,因爲它給了我意圖。使用標準,沒有通過新的意圖就會發生同樣的問題。 – Navarr 2012-02-03 19:38:22
標準問題仍然存在,即使我將意圖標記爲FLAG_ACTIVITY_NEW_TASK – Navarr 2012-02-03 20:43:54