如何从Android应用程序中的仪表板活动中了解出来?

时间:2018-09-02 10:33:59

标签: android android-fragments fragment-backstack

在我的android应用程序中,主要有三种功能,登录活动和仪表板活动。仪表板活动有5个片段,我添加到了backstack。我的应用程序每五分钟将用户位置传输到我的服务器。当用户登录时,我已经完成登录现在我想当用户在清除所有后退堆栈碎片后再按回去时,我的应用程序会像按主屏幕按钮一样最小化,它应该继续发送位置数据。当用户再次打开应用程序时,他应该处于仪表板活动中。这是我的信息中心活动代码。我们将不胜感激。请提供带有代码的解决方案。

package com.example.narmail.myapp.Api.Activities;

        import android.Manifest;
        import android.app.Activity;
        import android.content.DialogInterface;
        import android.content.Intent;
        import android.content.pm.PackageManager;
        import android.os.Build;
        import android.support.annotation.NonNull;
        import android.support.design.widget.NavigationView;
        import android.support.design.widget.NavigationView.OnNavigationItemSelectedListener;
        import android.support.v4.app.Fragment;
        import android.support.v4.app.FragmentManager;
        import android.support.v4.content.ContextCompat;
        import android.support.v4.view.GravityCompat;
        import android.support.v4.widget.DrawerLayout;
        import android.support.v7.app.ActionBarDrawerToggle;
        import android.support.v7.app.AlertDialog;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.support.v7.widget.Toolbar;
        import android.view.MenuItem;
        import android.view.View;
        import android.widget.Button;
        import android.widget.ProgressBar;
        import android.widget.Toast;

        import com.example.narmail.truck30mint.Api.Api.UserApi;
        import com.example.narmail.truck30mint.Api.Fragments.AddTruckFragment;
        import com.example.narmail.truck30mint.Api.Fragments.DashboardFragment;
        import com.example.narmail.truck30mint.Api.Fragments.ManageLoadFragment;
        import com.example.narmail.truck30mint.Api.Fragments.ManageTruckFragment;
        import com.example.narmail.truck30mint.Api.Fragments.PostLoadFragment;
        import com.example.narmail.truck30mint.Api.models.UserModels.User;
        import com.example.narmail.truck30mint.Api.services.LocationService;
        import com.example.narmail.truck30mint.Api.Api.TruckApi;
        import com.example.narmail.truck30mint.R;

        public class DashboardActivity extends AppCompatActivity implements OnNavigationItemSelectedListener{

        public static Button startDutyBtn;
        public static Button stopDutyBtn;
        public static Activity activity = null;
        public String token = null;
        public static Boolean truck = false;
        NavigationView navigationView;
        public static ProgressBar progressBar;
        DrawerLayout drawerLayout;
        Toolbar toolbar;
        ActionBarDrawerToggle actionBarDrawerToggle;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this;
        User user = new User(getApplicationContext());
        token = user.getToken();
        //  Toast.makeText(this,"in onCreate dasboard activity",Toast.LENGTH_SHORT).show();

        System.out.println("in dashboard activity token is "+token);
        setContentView(R.layout.activity_dashboard);

        setToolbar();
        startDutyBtn = findViewById(R.id.startDutyBtn);
        stopDutyBtn = findViewById(R.id.stopDutyBtn);
        progressBar = findViewById(R.id.dashboardProgressBar);
        TruckApi.hasUserTrucksForDashboardActivity(DashboardActivity.this);
        runtime_permissions();
        startDutyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i =new Intent(getApplicationContext(),LocationService.class);
                startService(i);
            }
        });

        stopDutyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(),LocationService.class);
                stopService(i);

            }
        });
        navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        // navigationView.OnNavigationItemSelectedListener(this);

        }

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        Fragment fragment = null;
        switch (item.getItemId()){
            case R.id.nav_home:
              //  finish();
                fragment = new DashboardFragment();
                break;
            case R.id.nav_post_load:
             fragment = new PostLoadFragment();
             break;
            case R.id.nav_add_truck:
               fragment = new AddTruckFragment();
               break;
            case R.id.nav_manage_truck:
                fragment = new ManageTruckFragment();
                break;
            case R.id.nav_manage_load:
                fragment = new ManageLoadFragment();
                break;
            case R.id.nav_signout:
                signoutAlert();
                break;

        }
        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.screen_area, fragment).addToBackStack(null).commit();
        }else{
            fragment = new DashboardFragment();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.screen_area, fragment).commit();
        }
        drawerLayout = findViewById(R.id.drawerLayout);
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
        }

        private void setToolbar(){
        drawerLayout = findViewById(R.id.drawerLayout);
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // toolbar.setNavigationIcon(R.drawable.back_arraow);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
        }

        /*   private void enable_buttons() {

        startDutyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i =new Intent(getApplicationContext(),LocationService.class);
                startService(i);
            }
        });

        stopDutyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent i = new Intent(getApplicationContext(),LocationService.class);
                stopService(i);

            }
        });
        }*/

        private boolean runtime_permissions() {
        if(Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){

            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},100);

            return true;
        }
        return false;
        }


        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 100){
            if( grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED){
             //   enable_buttons();
            }else {
                runtime_permissions();
            }
        }
        }

        /*    @Override
        public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
            getSupportFragmentManager().popBackStack();
        }else{
            new AlertDialog.Builder(this)
                    .setMessage("Are you sure you want to exit?")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            DashboardActivity.this.finish();
                        }
                    })
                    .setNegativeButton("No", null)
                    .show();
            super.onBackPressed();
        }
        }*/
        public void signoutAlert(){
        new AlertDialog.Builder(this)
                .setMessage("Are you sure you want to Signout?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        UserApi.logOut(DashboardActivity.this);
                    }
                })
                .setNegativeButton("No", null)
                .show();
        }

        }

0 个答案:

没有答案