Android: Back Button with Attach Data ใน Sdk Version 22

Android 23 ส.ค. 2015

ตอนนที่ใช้งานตอนนี้เป็น Sdk Version 22

import android.content.Intent;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import net.itorn.helpers.DebugHelper;

public class ServiceEvaluationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_evaluation);

        DebugHelper.LogStart("CustomerPasswordAuthActivity");

        // Back button
        if (getSupportActionBar() != null)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        initIntentData();
    }

    public void initIntentData() {

        Intent intent = getIntent();
        testData = intent.getStringExtra("TESTDATA");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_service_evaluation, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:

                NavUtils.navigateUpFromSameTask(this);
                onBackPressed();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void onBackPressed() {
        Intent intent = new Intent(ServiceEvaluationActivity.this, MyServiceChecklistOverviewListActivity.class);
        intent.putExtra("TESTDATA", testData);
        
        startActivity(intent);
    }
}

แท็ก

Onyx

Just a middle-aged programmer, Can do many things but not the most.