The title just about explains my entire problem. I have a simple news app – scroll up and down to look at article headlines, then click one to go to the ArticleActvity (ie read the article).
When the user swipes left or right, it loads the next or previous article. But – my code apparently just loads a new article activity on top of the other, because my back button, when clicked multiple times, will close the article activity, then close another, then close another….etc etc. until you finally get back to the list of articles.
As an Android noob, here’s my code to 1) detect a horizontal swipe, and 2) reload (attempt) the same activity with a new article.
Versions:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/>
My code in ArticleActivty.java:
SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener()
{
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
if(distanceX < -60)
{
//still need to hide article and show rotator graphic
Intent myIntent = new Intent(getBaseContext(), ArticleActivity.class);
myIntent.putExtra("id", String.valueOf(articlesDataSource.getNextOrPrevArticleId(article.id, true)));
overridePendingTransition(0, 0);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(myIntent);
}
else if(distanceX > 60)
{
//still need to hide article and show rotator graphic
Intent myIntent = new Intent(getBaseContext(), ArticleActivity.class);
myIntent.putExtra("id", String.valueOf(articlesDataSource.getNextOrPrevArticleId(article.id, false)));
overridePendingTransition(0, 0);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(myIntent);
}
return super.onScroll(e1, e2, distanceX, distanceY);
}
add
android:launchMode="singleInstance"in the
ArticleActivity'stag of your manifest file