我确信我这样做本来就是错的,但我想我还是会问。我似乎没有找到一个严格的答案,我正在为这样的问题做一切回答。所以,我认为它与Fragments或其他东西有关。我是Android开发的新手,所以请耐心等待。
所以,我有一个画廊视图。当您单击此库中的某个项目时,它会启动一个活动的意图,该活动在寻呼机中显示该项目以及其周围的其他项目。当您在操作栏上单击“主页”,或单击“后退”按钮,然后单击执行相同操作的其他图库视图时,新的视图会加载正常。
当你回到家中时,转到另一个应用程序,并长按主页按钮并点击我的应用程序,它加载得很好(虽然,并不总是在正确的地方,我认为是我需要添加的东西到savedInstanceState,但我离题了..)
但是,当我按下主页按钮时。点击启动器图标(启动mainActivity),然后点击其中一个库,它会在活动中显示OLD。这是问题所在,如果我能够在保持所有工作功能的同时从头开始活动,我可以纠正这个问题。
在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)。但是,它不是。
所以我的一般问题是:如何强制此活动开始新鲜。
我的另一个问题是:我在做什么本身就是错误的?
我对任何可能被视为“n00by”的事情表示哀悼和借口
答案 0 :(得分:1)
如果您确实需要重新启动活动,请覆盖onNewIntent(Intent)。
调用完成,然后为活动创建新意图。
答案 1 :(得分:-3)
使用onResume(),执行finish(),然后使用相同的Activity启动intent。