Application closing when I get call/change screen

Asked

Viewed 126 times

1

I’m developing an android app with the help of Android Studio, the app is a menu with 4 webviews.

Everything works out perfectly. My problem is that when I get a call and the app is open, as soon as I finish the call and hang up, the phone goes back to the Desktop screen, killing my app.

I would like, when closing the call, the app goes straight back to the last active screen of the app. But it is not only when I receive calls, when I open some other app by android bar along with my app, when closing the other application, it does not go back to mine, simply back to the Desktop screen.

import android.app.AlertDialog;
    import android.app.FragmentManager;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentTransaction;
    import android.view.View;
    import android.support.design.widget.NavigationView;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.webkit.WebView;
    import android.widget.TextView;


    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;


    import com.google.android.gms.ads.InterstitialAd;
    import com.google.android.gms.analytics.GoogleAnalytics;
    import com.google.android.gms.analytics.Tracker;


    public class MainActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {

        public static GoogleAnalytics analytics;
        public static Tracker tracker;

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

           analytics = GoogleAnalytics.getInstance(this);
            analytics.setLocalDispatchPeriod(1800);

            tracker = analytics.newTracker("UA-45783691-7");
            tracker.enableExceptionReporting(true);
            tracker.enableAdvertisingIdCollection(true);
            tracker.enableAutoActivityTracking(true);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
           // getSupportActionBar().setIcon(R.drawable.ic_logo);


            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);


        }

        @Override
        public void onBackPressed()
        {
            WebView webView = (WebView) findViewById(R.id.webview);
            if(webView.canGoBack()){
                webView.goBack();
            }else{
                new AlertDialog.Builder(this)
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setTitle("Fechar")
                        .setMessage("Tem certeza que deseja encerrar o aplicativo?")
                        .setPositiveButton("Sim", new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }

                        })
                        .setNegativeButton("Não", null)
                        .show();
            }
        }

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

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }

        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();
            Fragment fragment;

            if (id == R.id.nav_camera) {
                fragment = new SiteFragment();
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.mainFrame, fragment);
                ft.commit();


            } else if (id == R.id.nav_gallery) {
                fragment = new ForumActivity();
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.mainFrame, fragment);
                ft.commit();

            } else if (id == R.id.nav_slideshow) {
                fragment = new LojaActivity();
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.mainFrame, fragment);
                ft.commit();

            } else if (id == R.id.nav_manage) {
                fragment = new ClassificadosActivity();
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.mainFrame, fragment);
                ft.commit();

            } else if (id == R.id.nav_share) {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, "Eu estou usando o APP  para android. Baixe você também em: ");
                sendIntent.setType("text/plain");
                startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));

            } else if (id == R.id.nav_send) {

            }
            else if (id == R.id.chat_mobile) {
                fragment = new ChatFragment();
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.mainFrame, fragment);
                ft.commit();
            }


            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;

        }
@Override
    public void onResume() {
        super.onResume();

    }

    }
  • 2

    Enter the code of the screen that presents this problem. In particular, the onCreate and the onResume.

  • I include the Mainactiv code, the others are fragments of main.. The closure of when you answer calls, occurs either with the app opened on Main or in any Fragment. Remembering that the APP works 100%. The only problem is the closure.

  • 1

    Marko, snnipet is more recommended for codes that can be simulated and rendered here, such as hmtl, css and javascript. For other codes, use the {} to format.

  • It’s I had trying to use, but every time I pasted the code a part was left out.. But now it worked. Thanks.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.