说明:
我有导航抽屉,其中有多个片段。像HomeFragment
,firstFragment
,secondFragment
等。
在HomeFragment
中,我调用了一个适配器,在其中我将一个意图从适配器传递给另一个活动。
当我按下活动应用程序的后退按钮时,直接关闭而不是后退。
这是我的适配器,我将意图传递给另一个Activity
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String key=lst_key.get(position);
String status_value=list_status.get(position);
if(status_value.equals("notstarted")){
Log.e("MATCH_KEY",""+key);
Intent intent=new Intent(context,NotStartedMatchDetails.class);
intent.putExtra("mainobj", lst.get(position).toString());
context.startActivity(intent);
// MainActivity mainActivity=(MainActivity)context;
// Intent intent=new Intent(context,SummaryCard.class);
// intent.putExtra("Key",key);
// intent.putExtra("access_token",mainActivity.getMyData());
// context.startActivity(intent);
}
if(status_value.equals("started")){
Log.e("MATCH_KEY",""+key);
MainActivity mainActivity=(MainActivity)context;
Intent intent=new Intent(context,SummaryCard.class);
intent.putExtra("Key", key);
intent.putExtra("access_token",mainActivity.getMyData());
context.startActivity(intent);
}
}
});
这是我的活动
public class NotStartedMatchDetails extends AppCompatActivity {
Toolbar toolbar;
String str="";
TextView txtA_team_name;
TextView txtB_team_name;
TextView txtDate;
TextView txtVanue;
TextView txtMatch_title;
Typeface tf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_not_started_match_details);
toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
tf=Typeface.createFromAsset(this.getResources().getAssets(), "Roboto-Regular.ttf");
txtA_team_name=(TextView)findViewById(R.id.first_team_name);
txtB_team_name=(TextView)findViewById(R.id.second_team_name);
txtDate=(TextView)findViewById(R.id.date);
txtVanue=(TextView)findViewById(R.id.vanue);
txtA_team_name.setTypeface(tf,Typeface.BOLD);
txtB_team_name.setTypeface(tf,Typeface.BOLD);
txtDate.setTypeface(tf);
txtVanue.setTypeface(tf);
FrameLayout frameLayout=(FrameLayout)findViewById(R.id.adbar);
new AddLoader(this).LoadAds(frameLayout);
Bundle bundle=getIntent().getExtras();
str=(String) bundle.get("mainobj");
String key=(String)bundle.get("Key");
Date date=null;
String ISTdateTime="";
try{
JSONObject mainObj=new JSONObject(str);
String name = mainObj.getString("name");
String vanue=mainObj.getString("venue");
String title=mainObj.getString("title");
JSONObject start_date=mainObj.getJSONObject("start_date");
String srt_date=start_date.getString("iso");
try{
DateFormat format=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'+00':ss");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
date=format.parse(srt_date);
// Sat Mar 19 19:30:00 GMT+05:30 2016
String[] name_parts;
String[] title_parts;
name_parts=name.split("vs");
title_parts=title.split("-");
ISTdateTime=date.toString();
String[] ISTPARTS=ISTdateTime.split(" ");
String ISTdate=ISTPARTS[1]+" "+ISTPARTS[2];
String ISTime=ISTPARTS[3];
String[] Time_parts=ISTime.split(":");
String hours=Time_parts[0];
String minutes=Time_parts[1];
String GMTDate=srt_date;
String[] GMT_parts=GMTDate.split("T");
String gmt_time = GMT_parts[1];
String[] gmt_hour_parts = gmt_time.split("\\+");
String fullTime=ISTdate+","+hours+":"+minutes+" IST | "+gmt_hour_parts[0]+" GMT";
txtA_team_name.setText(name_parts[0]);
txtB_team_name.setText(name_parts[1]);
txtDate.setText(fullTime);
txtVanue.setText(vanue);
getSupportActionBar().setTitle(title_parts[0]);
}
catch (Exception e){
e.printStackTrace();
}
}
catch (JSONException e){
e.printStackTrace();
}
try{
SimpleDateFormat old_format =new SimpleDateFormat("HH:mm:ss");
//oldDate.setTimeZone(TimeZone.getTimeZone("UTC"));
Date d1;
Date d2=null;
// Log.e("DAte",""+date.toString());
// d1=oldDate.parse(oldDate.format(date));
// Log.e("OLD DATE",""+d1.toString());
d2=old_format.parse(old_format.format(new Date()));
Log.e("new date",""+d2.toString());
}
catch (Exception e){
e.printStackTrace();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
Intent parentIntent = NavUtils.getParentActivityIntent(this);
parentIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(parentIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的manifiest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.angelnx.cricvilla.cricvilla">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyMaterialTheme">
<activity
android:name=".SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"></activity>
<activity
android:name=".NotStartedMatchDetails"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"></meta-data>
</activity>
<activity
android:name=".SummaryCard"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait"
android:theme="@style/MyMaterialTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".ScoreCard"
android:parentActivityName=".SummaryCard"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".SummaryCard" />
</activity>
<activity
android:name=".PlayerInfoDetails"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".FullScore"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name=".PlayerIccStats"></activity>
</application>
</manifest>
问题是当我按下我的活动的后退按钮时,它直接终止应用程序而不是转到homeFragment
。
请帮我解决这个问题。
答案 0 :(得分:2)
您在活动中使用意图标记将您的活动置于最前面,parentIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
这可能是您的应用程序关闭的原因,因为现在您的活动位于最前端,并且没有剩余的活动后备堆栈。