I’m sure I’m doing this inherently wrong, but I figured I’d ask anyway. I haven’t seemed to find a strict answer to this, and I’m doing everything answers to questions like this say to do. So, I figure its something to do with Fragments or something. I’m fairly new to Android development, so please bear with me.
So, I have a gallery view. When you click one of the items in this gallery it starts an intent for an Activity that displays that item in a pager with the other items surrounding it. When you click home on the action bar, or hit the back button and then click a different gallery view that does the same thing, the new one loads up fine.
When you hit home, go to another app, and long-hold the home button and hit the app I am, it loads up fine (albeit, not always in the right place which I figure is something I just need to add to savedInstanceState, but I digress..)
However, when I hit the home button. Hit the launcher icon (which starts the mainActivity) and then hit one of the galleries, it shows the OLD one in the activity. This is the problem, and one I could rectify if I could get the activity to start from scratch while maintaining all of the working functionality.
In the XML, the activity is specified as such:
<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>
I’m calling the intent as such:
// 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);
and the activity calling this new activity/intent is defined as the following:
<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>
and my activity itself uses the code below:
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);
}
}
}
I’m setting the new intent, and calling all the things I normally call, so the way I’m thinking it should “just work” (TM). However, it’s NOT.
So my general question is: how do I force this activity to start FRESH.
and my alternative question is: what am I doing that’s inherently wrong?
I offer my general condolences and excuses for anything that may be perceived as “n00by”
If you have a definitive need to restart your activity, override onNewIntent(Intent).
Call finish, and then create a new intent for the activity.