Milan Gajera
Milan Gajera

Reputation: 970

Why application closed after pressing back button of activity?

Explanation: I have navigation drawer in which there are multiple fragments.Like HomeFragment, firstFragment, secondFragment and etc. In HomeFragment, I called a adapter in which I passed an intent from adapter to another activity.

When I pressed a back button of activity application directly closed instead of back.

here is my adapter where I passed an intent to another 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);
                }
            }
        });

Here is my activity

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

Here is my manifiest.xml file

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

Problem is when I pressed a back button of my activity it directly goes to terminate a application instead of goes to homeFragment.

Please help me to solve out this problem.

Upvotes: 0

Views: 215

Answers (1)

Deepak Sharma
Deepak Sharma

Reputation: 417

You are using intent flags in your activity to bring your activity to the front, parentIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); This might be the reason why your application closes because now your activity is in the front-most and there are no more back stacks of activities left.

Upvotes: 2

Related Questions