当有人按下Fragment
中的按钮但我无法正常工作时,我只想得到一个特定的Activity
。请帮我解决这个问题。
这是我的Activity
TopicDisplay.java
package androidthirst.company.abhi.totalenglish;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebView;
public class TopicDisplay extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_topic_display);
String data = getIntent().getExtras().getString("first");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
toolbar.setTitle(data);
toolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.mipmap.ic_arrow_back_black_36dp);
WebView tabViewing = (WebView) findViewById(R.id.webView);
tabViewing.loadUrl(getIntent().getDataString());
}
}
我想要从Fragment
Activity
代码
package androidthirst.company.abhi.totalenglish;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Tab1Fragment extends Fragment {
ListView listView;
String[] image1;
String[] mainTopic;
String[] mainDescription;
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
getActivity().onBackPressed();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
Resources res = getResources();
mainTopic = res.getStringArray(R.array.MainTopic);
mainDescription = res.getStringArray(R.array.DescriptionContent);
listView = (ListView) view.findViewById(R.id.listView);
MyAdapter myAdapter = new MyAdapter(getActivity(), mainTopic, image1, mainDescription);
listView.setAdapter(myAdapter);
// toolbar.setTitleTextColor(Color.WHITE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
Intent i0 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
getActivity().overridePendingTransition(0, 0);
i0.putExtra("first", "Active Passive");
Uri uri0 = Uri.parse("file:///android_asset/ActivePassive.html");
i0.setData(uri0);
startActivity(i0);
break;
case 1:
Intent i1 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
getActivity().overridePendingTransition(0, 0);
i1.putExtra("first", "Adjective");
Uri uri1 = Uri.parse("file:///android_asset/Adjective.html");
i1.setData(uri1);
startActivity(i1);
break;
case 2:
Intent i2 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i2.putExtra("first", "Adverb");
Uri uri2 = Uri.parse("file:///android_asset/adverb.html");
i2.setData(uri2);
startActivity(i2);
break;
case 3:
Intent i3 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i3.putExtra("first", "Article");
Uri uri3 = Uri.parse("file:///android_asset/article.html");
i3.setData(uri3);
startActivity(i3);
break;
case 4:
Intent i4 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i4.putExtra("first", "Clause");
Uri uri4 = Uri.parse("file:///android_asset/clause.html");
i4.setData(uri4);
startActivity(i4);
break;
case 5:
Intent i5 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
Uri uri5 = Uri.parse("file:///android_asset/conjunction.html");
i5.putExtra("first", "Conjunction");
i5.setData(uri5);
startActivity(i5);
break;
case 6:
Intent i6 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i6.putExtra("first", "Degree");
Uri uri6 = Uri.parse("file:///android_asset/degree.html");
i6.setData(uri6);
startActivity(i6);
break;
case 7:
Intent i7 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i7.putExtra("first", "Determiner");
Uri uri7 = Uri.parse("file:///android_asset/determiner.html");
i7.setData(uri7);
startActivity(i7);
break;
case 8:
Intent i8 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i8.putExtra("first", "Direct Indirect");
Uri uri8 = Uri.parse("file:///android_asset/direct.html");
i8.setData(uri8);
startActivity(i8);
break;
case 9:
Intent i9 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i9.putExtra("first", "Figur of Speech");
Uri uri9 = Uri.parse("file:///android_asset/figur.html");
i9.setData(uri9);
startActivity(i9);
break;
case 10:
Intent i10 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i10.putExtra("first", "Gender");
Uri uri10 = Uri.parse("file:///android_asset/gender.html");
i10.setData(uri10);
startActivity(i10);
break;
case 11:
Intent i11 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i11.putExtra("first", "Hymonyms");
Uri uri11 = Uri.parse("file:///android_asset/homonyms.html");
i11.setData(uri11);
startActivity(i11);
break;
case 12:
Intent i12 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i12.putExtra("first", "Idoms");
Uri uri12 = Uri.parse("file:///android_asset/idoms.html");
i12.setData(uri12);
startActivity(i12);
break;
case 13:
Intent i13 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i13.putExtra("first", "Noun");
Uri uri13 = Uri.parse("file:///android_asset/noun.html");
i13.setData(uri13);
startActivity(i13);
break;
case 14:
Intent i14 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i14.putExtra("first", "Plural");
Uri uri14 = Uri.parse("file:///android_asset/plural.html");
i14.setData(uri14);
startActivity(i14);
break;
case 15:
Intent i15 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i15.putExtra("first", "Preposition");
Uri uri15 = Uri.parse("file:///android_asset/preposition.html");
i15.setData(uri15);
startActivity(i15);
break;
case 16:
Intent i16 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i16.putExtra("first", "Pronoun");
Uri uri16 = Uri.parse("file:///android_asset/pronoun.html");
i16.setData(uri16);
startActivity(i16);
break;
case 17:
Intent i17 = new Intent(getActivity().getApplicationContext(), TopicDisplay.class);
i17.putExtra("first", "Verbs");
Uri uri17 = Uri.parse("file:///android_asset/verbs.html");
i17.setData(uri17);
startActivity(i17);
break;
}
}
});
return view;
}
}
答案 0 :(得分:2)
在getActivity().onBackPressed()
中调用onBackPressed()
在这里没有意义,您可以轻松进入无限循环。
您可能需要在super.onBackPressed()
区块中致电else
:
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
通过使用super
关键字,可以调用超类的onBackPressed()
方法(如果后栈是空的)。
答案 1 :(得分:0)