Java 安卓-清除意图
我的问题与这个问题有关 但我在实施上遇到了问题 我今天的第一堂课是java,它有一个按钮,当我点击按钮时,我会输入额外的Java 安卓-清除意图,java,android,android-intent,Java,Android,Android Intent,我的问题与这个问题有关 但我在实施上遇到了问题 我今天的第一堂课是java,它有一个按钮,当我点击按钮时,我会输入额外的intent.putExtra(“highlegs”,“highlegs”)和起始触觉(意图) TodaySercise.java final Intent intent = getIntent(); if (intent != null) { String clicked = intent.getStringExtra("button");
intent.putExtra(“highlegs”,“highlegs”)代码>和起始触觉(意图)代码>
TodaySercise.java
final Intent intent = getIntent();
if (intent != null) {
String clicked = intent.getStringExtra("button");
if (clicked.equals("btn1")) {
intent.setClass(TodaysExercise.this, DoExercise.class);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//When btn1 is clicked I putExtra
intent.putExtra("highlegs", "High Legs");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_do_the_exercise);
textView = (TextView) findViewById(R.id.replace_this);
descheader = (TextView) findViewById(R.id.desc_header_id);
fab = (FloatingActionButton) findViewById(R.id.fab);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/countdown.ttf");
descheader.setTypeface(custom_font);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String text = bundle.getString("highlegs");
if (text != null)
textView.setText(text);
}
if (textView.getText().equals("High Legs"))
{
ImageView imLoading = (ImageView) findViewById(R.id.loadingView);
imLoading.setBackgroundResource(R.drawable.workout);
AnimationDrawable frameAnimation = (AnimationDrawable) imLoading
.getBackground();
frameAnimation.start();
final Intent i = new Intent();
i.setClass(DoExercise.this, ReadyForNext.class);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i.putExtra("next", "Leg Curls X 20");
startActivity(i);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
}
});
}
下一个类DoExercise.java
然后获取字符串string text=bundle.getString(“highlegs”)
并将其设置为textViewtextView.setText(text)代码>。然后我检查TextView是否等于if(TextView.getText().equals(“High Legs”)
,如果是,我再次输入extraI.putExtra(“next”,“Leg Curls X 20”)代码>并开始下一节课开始活动(意图)代码>
DoExercise.java
final Intent intent = getIntent();
if (intent != null) {
String clicked = intent.getStringExtra("button");
if (clicked.equals("btn1")) {
intent.setClass(TodaysExercise.this, DoExercise.class);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//When btn1 is clicked I putExtra
intent.putExtra("highlegs", "High Legs");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_do_the_exercise);
textView = (TextView) findViewById(R.id.replace_this);
descheader = (TextView) findViewById(R.id.desc_header_id);
fab = (FloatingActionButton) findViewById(R.id.fab);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/countdown.ttf");
descheader.setTypeface(custom_font);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String text = bundle.getString("highlegs");
if (text != null)
textView.setText(text);
}
if (textView.getText().equals("High Legs"))
{
ImageView imLoading = (ImageView) findViewById(R.id.loadingView);
imLoading.setBackgroundResource(R.drawable.workout);
AnimationDrawable frameAnimation = (AnimationDrawable) imLoading
.getBackground();
frameAnimation.start();
final Intent i = new Intent();
i.setClass(DoExercise.this, ReadyForNext.class);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i.putExtra("next", "Leg Curls X 20");
startActivity(i);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
}
});
}
我得到stringtext=bundle.getString(“下一步”)
和settv_nextdesc.setText(text)代码>字符串附加到文本视图。我在这个类中还有一个按钮,应该返回到DoExercise.java
,这就是我的问题所在
ReadyForNext.java
public class ReadyForNext extends AppCompatActivity {
Button btn_next_exercise;
TextView tv_nextdesc;
TextView tv_nexttxt;
Context f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ready_for_next);
btn_next_exercise = (Button) findViewById(R.id.btn_next_exercise);
tv_nextdesc = (TextView) findViewById(R.id.tv_nextdesc);
tv_nexttxt = (TextView) findViewById(R.id.nxtTxt);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/countdown.ttf");
tv_nextdesc.setTypeface(custom_font);
tv_nexttxt.setTypeface(custom_font);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String text = bundle.getString("next");
if (text != null)
tv_nextdesc.setText(text);
}
final Intent i = new Intent();
i.setClass(ReadyForNext.this, DoExercise.class);
btn_next_exercise.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//I first check what the title is to determine what to put extra
if (tv_nextdesc.getText().equals("Leg Curls X 20")) {
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
//then here I putExtra
i.putExtra("legcurls", "Leg Curls");
startActivity(i);
}
}
});
}
正如你所见,我有三个类基本上是循环运行的,todaysercise.java
打开DoExercise.java
,它打开ReadyForNext.java
,从这里我想再次打开DoExercise.java
(重用DoExercise.java),但那时候我想清除添加新意图的意图
关于如何实现这一点有什么帮助吗?请在此处完成您的DoExercise活动
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i.putExtra("next", "Leg Curls X 20");
startActivity(i);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
}
});
像你现在做的那样,从ReadyForNext活动开始
现在您必须在DoExercise类中进行一些更改,因为您的意图是在不同的类中更改额外的键,否则您将得到null pointerException
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if(bundle.containsKey("highlegs")){
String text = bundle.getString("highlegs");
}
if(bundle.containsKey("legcurls")){
String text = bundle.getString("legcurls");
}
if (text != null)
textView.setText(text);
}
希望这能对你有所帮助。在这里完成你的DoExercise活动
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i.putExtra("next", "Leg Curls X 20");
startActivity(i);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
}
});
像你现在做的那样,从ReadyForNext活动开始
现在您必须在DoExercise类中进行一些更改,因为您的意图是在不同的类中更改额外的键,否则您将得到null pointerException
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if(bundle.containsKey("highlegs")){
String text = bundle.getString("highlegs");
}
if(bundle.containsKey("legcurls")){
String text = bundle.getString("legcurls");
}
if (text != null)
textView.setText(text);
}
希望这能对您有所帮助。您的活动被调整为:A->B->C,然后您希望删除C并更新活动B。建议1:如果这三个屏幕共同负责单个任务,最好使用片段而不是活动。使用片段处理这种情况会更容易。建议2:从B->C移动时,调用this.finish();在活动B和活动C中,使用一组新的bundle向C激发一个新的意图
和textView.setText(text)所以当我从“C”-“ReadyForNext.java”返回到那个活动时,它会再次调用它。我需要一种方法来清除该intentExtra并调用新的intentExtra。您的活动被关联为:a->B->C,然后您希望删除C并更新活动B。建议1:如果这三个屏幕共同负责单个任务,最好使用片段而不是活动。使用片段处理这种情况会更容易。建议2:从B->C移动时,调用this.finish();在活动B和活动C中,使用一组新的bundle向C激发一个新的意图
和textView.setText(text)所以当我从“C”-“ReadyForNext.java”返回到那个活动时,它会再次调用它。我需要一种方法来清除该intentExtra并调用新的intentExtra。但是我在哪里获取string和settext到legcolls
?在您的代码中,您只声明它是否是,这意味着它将始终是highlegs
,并且Legcolls
将永远不会被调用?我已经让它工作了,谢谢。我使用了您的代码并放置了stringtext2=bundle.getString(“legcolls”)下面的代码>如果(bundle!=null){
但是我在哪里获取字符串和settext到legcolls
?在你的代码中,你只声明如果它是,这意味着它将永远是highlegs
并且legcolls
将永远不会被调用?我让它工作了,谢谢你。我使用了你的代码并放置了String text2=bundle.getstring(如果(bundle!=null){