gpt4 book ai didi

android - 如何强制 Activity 从头开始​​?

转载 作者:行者123 更新时间:2023-11-29 14:17:27 26 4
gpt4 key购买 nike

我确定我这样做本质上是错误的,但我想我还是要问一下。我似乎还没有找到一个严格的答案,我正在尽一切努力回答这样的问题。所以,我认为它与 Fragments 或其他东西有关。我是 Android 开发的新手,所以请多多包涵。

所以,我有一个画廊 View 。当您单击此库中的其中一项时,它会启动一个 Activity 的 Intent ,该 Activity 会在寻呼机中显示该项目以及围绕它的其他项目。当您单击操作栏上的主页,或点击后退按钮,然后单击执行相同操作的不同图库 View 时,新的会正常加载。

当你回到家,转到另一个应用程序,并长按主页按钮并点击我正在使用的应用程序时,它加载正常(尽管并不总是在正确的位置,我认为这是我只需要添加的东西到 savedInstanceState,但我离题了..)

但是,当我按下主页按钮时。点击启动器图标(启动 mainActivity),然后点击其中一个画廊,它显示 Activity 中的旧画廊。这就是问题所在,如果我可以让 Activity 从头开始,同时保持所有工作功能,我可以纠正这个问题。

在 XML 中, Activity 是这样指定的:

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

我这样称呼 Intent :

// 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/Intent 的 Activity 定义如下:

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

我的 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);
}
}
}

我正在设置新的 Intent ,并调用我通常调用的所有东西,所以我认为它应该“正常工作”(TM) 的方式。然而,事实并非如此。

所以我的一般问题是:如何强制此 Activity 重新开始。

我的另一个问题是:我在做什么本质上是错误的?

我对任何可能被视为“n00by”的事情表示普遍的哀悼和借口

最佳答案

如果您确实需要重新启动 Activity,请覆盖 onNewIntent(Intent)。

调用 finish,然后为 Activity 创建一个新的 Intent 。

关于android - 如何强制 Activity 从头开始​​?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9133228/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com