应用程序似乎在启动画面后以横向模式启动不止一次?

古尼特·考尔

我有一个启动画面,之后我的主要活动开始了。正常工作在纵向模式,但如果万一我在倾斜我的手机风景模式中,主要活动可以看出推出不止一次启动画面后

我尝试使用 android:launchMode="singleInstance" 但在这种情况下,我无法在反馈警报框中附加文件。

以下是我的代码:

主活动.java

package com.example.android.tel;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.crashlytics.android.Crashlytics;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MainActivity extends AppCompatActivity {
    Toolbar mActionBarToolbar;
    TextView toolbar_title_mainActivity, main_textView, disclaimer_txtView;
    CardView SearchDept, SearchName, disclaimer, feedback;
    ImageView back;
    ArrayList<Uri> arrayUri = new ArrayList<Uri>();
    ArrayAdapter<Uri> myFileListAdapter;
    ListView listViewFiles;
    Dialog alertDialog;

    final int RQS_LOADIMAGE = 0;
    final int RQS_SENDEMAIL = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if (getResources().getConfiguration().orientation ==
                Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.activity_main);
        } else {
            setContentView(R.layout.activity_main);
        }

        mActionBarToolbar = (Toolbar) findViewById(R.id.tool_bar_main_activity);
        toolbar_title_mainActivity = (TextView) findViewById(R.id.toolbar_title);
        main_textView = (TextView) findViewById(R.id.main_textView);
        main_textView.setPaintFlags(main_textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        setSupportActionBar(mActionBarToolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar_title_mainActivity.setText("Hry. Govt. Telephone Directory");
        back = (ImageView) findViewById(R.id.back);
        back.setVisibility(View.INVISIBLE);
        disclaimer = (CardView) findViewById(R.id.disclaimer);
        feedback = (CardView) findViewById(R.id.feedback);

        SearchDept = (CardView) findViewById(R.id.cardView1_mainActivity);
        SearchName = (CardView) findViewById(R.id.cardView2_mainActivity);
        SearchDept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, CardViewActivity.class);
                startActivity(i);
            }
        });

        SearchName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent j = new Intent(MainActivity.this, ByNameListActivity.class);
                startActivity(j);
            }
        });

        disclaimer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog alertDialog = new Dialog(MainActivity.this);
                alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                alertDialog.setContentView(R.layout.disclaimer);
                alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
                alertDialog.show();
            }
        });

        feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 alertDialog = new Dialog(MainActivity.this);
                alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                alertDialog.setContentView(R.layout.feedback);
                alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
                alertDialog.setCanceledOnTouchOutside(true);

                ImageView send_btn=(ImageView)alertDialog.findViewById(R.id.send);
                ImageView attach_btn=(ImageView)alertDialog.findViewById(R.id.attachment);
                final TextView to_email_add=(TextView)alertDialog.findViewById(R.id.email_address);
                to_email_add.setText("[email protected]");
                final EditText email_subject=(EditText)alertDialog.findViewById(R.id.email_subject);
                final EditText email_text=(EditText)alertDialog.findViewById(R.id.email_text);
                final EditText mobile_no=(EditText)alertDialog.findViewById(R.id.mobile_text);

                email_subject.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (!hasFocus) {
                            hideKeyboard(v);
                        }
                    }
                });

                email_text.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (!hasFocus) {
                            hideKeyboard(v);
                        }
                    }
                });

                myFileListAdapter = new ArrayAdapter<Uri>(
                        MainActivity.this,
                        android.R.layout.simple_list_item_1,
                        arrayUri);
                listViewFiles = (ListView)alertDialog.findViewById(R.id.filelist);
                listViewFiles.setAdapter(myFileListAdapter);

                listViewFiles.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                        myFileListAdapter.remove(arrayUri.get(position));
                        myFileListAdapter.notifyDataSetChanged();
                        Toast.makeText(view.getContext(), "You unattached one item", Toast.LENGTH_LONG).show();
                        return false;
                    }
                });



                attach_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(Intent.ACTION_PICK,
                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(intent, RQS_LOADIMAGE);

                    }
                });


                send_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String email_add=to_email_add.getText().toString();
                        String email_sub=email_subject.getText().toString();
                        String email_txt=email_text.getText().toString();
                        String emailAddressList[] = {email_add};
                        String mobileNo=mobile_no.getText().toString();
                        String info=email_txt+"\n\nPhone Number :"+mobileNo;


                        Intent intent = new Intent();
                        intent.putExtra(Intent.EXTRA_EMAIL, emailAddressList);
                        intent.putExtra(Intent.EXTRA_SUBJECT, email_sub);
                        intent.putExtra(Intent.EXTRA_TEXT,info);

                        if(arrayUri.isEmpty()&& isValidPhone(mobileNo)&& !(mobileNo.isEmpty())){
                            //send email without photo attached
                            intent.setAction(Intent.ACTION_SEND);
                            intent.setType("plain/text");

                            new Handler().postDelayed(new Runnable() {

                                public void run() {
                                    alertDialog.dismiss();
                                }
                            }, 5000);

                        }else if(arrayUri.size() == 1 && isValidPhone(mobileNo)&& !(mobileNo.isEmpty())){
                            //send email with ONE photo attached
                            intent.setAction(Intent.ACTION_SEND);
                            intent.putExtra(Intent.EXTRA_STREAM, arrayUri.get(0));
                            intent.setType("image/*");

                            new Handler().postDelayed(new Runnable() {

                                public void run() {
                                    alertDialog.dismiss();
                                }
                            }, 5000);

                        }else if(arrayUri.size()>1&& isValidPhone(mobileNo)&& !(mobileNo.isEmpty())){
                            //send email with MULTI photo attached
                            intent.setAction(Intent.ACTION_SEND_MULTIPLE);
                            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayUri);
                            intent.setType("image/*");

                            new Handler().postDelayed(new Runnable() {

                                public void run() {
                                    alertDialog.dismiss();
                                }
                            }, 5000);

                        }
                        else {
                            Toast.makeText(v.getContext(), "Phone number is not valid", Toast.LENGTH_LONG).show();
                        }

                        startActivity(Intent.createChooser(intent, "Please provide valid details"));

                    }
                });

                alertDialog.show();
            }
        });



    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK){
            switch(requestCode){
                case RQS_LOADIMAGE:
                    Uri imageUri = data.getData();
                    arrayUri.add(imageUri);
                    myFileListAdapter.notifyDataSetChanged();
                    break;
                case RQS_SENDEMAIL:
                    break;
            }
        }
    }

    public static boolean isValidPhone(String phone)
    {
        String expression = "^([0-9\\+]|\\(\\d{1,3}\\))[0-9\\-\\. ]{3,15}$";
        CharSequence inputString = phone;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputString);
        if (matcher.matches())
        {
            return true;
        }
        else{
            return false;
        }
    }

    public void hideKeyboard(View view) {
        InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        int orientation;
        if (getResources().getConfiguration().orientation ==
                Configuration.ORIENTATION_PORTRAIT) {
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            // or = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }else {
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
        }
        // Add code if needed
//        listViewFiles.setAdapter(myFileListAdapter);
//        myFileListAdapter.notifyDataSetChanged();
        setRequestedOrientation(orientation);
    }


}

活动_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:weightSum="14"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    tools:context="com.example.android.tel.MainActivity">

    <include
        android:id="@+id/tool_bar_main_activity"
        layout="@layout/toolbar">
    </include>

    <TextView
        android:id="@+id/main_textView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:layout_marginTop="10dp"
        android:text="How would you like to search?"
        android:textStyle="bold"
        android:textSize="14sp"
        android:textColor="#1A237E"
        android:gravity="center_horizontal"/>

    <RelativeLayout
        android:id="@+id/searchby_btns"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:gravity="center_vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/cardView1_mainActivity"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        card_view:cardCornerRadius="4dp"
        android:layout_marginTop="10dp"
        card_view:cardBackgroundColor="#e97c1d">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search By Department.."
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="18sp"
            android:layout_centerInParent="true"/>
        </RelativeLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:id="@+id/cardView2_mainActivity"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_below="@id/cardView1_mainActivity"
        card_view:cardCornerRadius="4dp"
        android:layout_marginTop="20dp"
        card_view:cardBackgroundColor="#e97c1d">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search By Name.."
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="18sp"
            android:layout_centerInParent="true"/>

        </RelativeLayout>

    </android.support.v7.widget.CardView>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="8.5">
    <ImageView
        android:id="@+id/map_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/hry_map"
        android:elevation="4dp"
        android:layout_gravity="center"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_below="@id/map_image"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingRight="16dp"
        android:paddingLeft="16dp"
        android:layout_marginTop="2dp"
        android:gravity="center_horizontal"
        android:layout_alignParentBottom="true">

        <android.support.v7.widget.CardView
            android:id="@+id/disclaimer"
            android:layout_width="150dp"
            android:layout_height="30dp"
            card_view:cardCornerRadius="4dp"
            card_view:cardElevation="4dp"
            android:layout_marginRight="8dp"
            card_view:cardBackgroundColor="#424242">
            <TextView
                android:layout_width="150dp"
                android:layout_height="30dp"
                android:text="Disclaimer"
                android:gravity="center"
                android:layout_gravity="center_vertical"
                android:textColor="#FFFFFF"
                android:textStyle="bold"/>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:id="@+id/feedback"
            android:layout_toRightOf="@id/disclaimer"
            android:layout_width="150dp"
            android:layout_height="30dp"
            card_view:cardCornerRadius="4dp"
            card_view:cardElevation="4dp"
            card_view:cardBackgroundColor="#424242">
            <TextView
                android:layout_width="150dp"
                android:layout_height="30dp"
                android:text="Feedback"
                android:gravity="center"
                android:layout_gravity="center_vertical"
                android:textColor="#FFFFFF"
                android:textStyle="bold"/>
        </android.support.v7.widget.CardView>

    </RelativeLayout>

</LinearLayout>

SplashScreenActivity.java

package com.example.android.telephonedirectory;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;

import com.felipecsl.gifimageview.library.GifImageView;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;

public class SplashScreenActivity extends AppCompatActivity {
//    private GifImageView gifimageview;
    private ProgressBar progressBarSplashScreen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getResources().getConfiguration().orientation ==
                Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.activity_splash_screen);
        } else {
            setContentView(R.layout.activity_splash_screen);
        }

//        gifimageview=(GifImageView)findViewById(R.id.gifSplashscreenImage);
        progressBarSplashScreen=(ProgressBar)findViewById(R.id.progressbarSplashscreen);
        progressBarSplashScreen.setVisibility(progressBarSplashScreen.VISIBLE);

        //set GifImageView Resource

        /*try {
            InputStream inputStream=getAssets().open("splash_Screen.png");
            byte[] bytes= IOUtils.toByteArray(inputStream);
            gifimageview.setBytes(bytes);
            gifimageview.startAnimation();
        }catch (IOException ex){

        }*/

        //Wait for 4 seconds and start activity main
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                SplashScreenActivity.this.startActivity(new Intent(SplashScreenActivity.this,MainActivity.class));
                SplashScreenActivity.this.finish();
            }
        },2000);
    }

    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        int orientation;
        if (getResources().getConfiguration().orientation ==
                Configuration.ORIENTATION_PORTRAIT) {
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            // or = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }else {
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
        }
        // Add code if needed
//        listViewFiles.setAdapter(myFileListAdapter);
//        myFileListAdapter.notifyDataSetChanged();
        setRequestedOrientation(orientation);
    }
}
切坦·乔希

不要使用 android:launchMode="singleInstance"

您应该"singleTask"为此使用启动模式

因为singleInstance为 Activity 创建单独的任务堆栈,并且不检查当前任务堆栈中的活动。

"singleTask"每一次检查,如果在任务堆栈的活动存在,不能创建新的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

启动画面显示不止一次

来自分类Dev

我在独立应用程序expo-deeplink上进行测试后,调度功能似乎运行了不止一次

来自分类Dev

如何使SwingWorker启动不止一次?

来自分类Dev

应用程序:didReceiveRemoteNotification:fetchCompletionHandler:调用不止一次。如何避免?

来自分类Dev

停止播放动画不止一次

来自分类Dev

应用程序停止在启动画面

来自分类Dev

CSS样式不止一次应用

来自分类Dev

应用程序崩溃后重新启动一次方案

来自分类Dev

iOS 8-以横向模式启动应用程序

来自分类Dev

创建启动程序以一次启动多个应用程序?

来自分类Dev

创建启动程序以一次启动多个应用程序?

来自分类Dev

警报返回不止一次

来自分类Dev

评论打印不止一次

来自分类Dev

从管道读取不止一次

来自分类Dev

如何仅显示一次登录,然后直接在android中启动应用程序后显示

来自分类Dev

在我的应用程序中制作启动画面后没有操作栏

来自分类Dev

Ionic 3 在启动画面后需要很长时间才能进入应用程序

来自分类Dev

在ionic 2应用程序中删除启动画面

来自分类Dev

OSX Cocoa应用程序中的启动画面

来自分类Dev

Android应用程序在Unity启动画面上崩溃

来自分类Dev

iOS 移动应用程序中的启动画面(Swift)

来自分类Dev

带有 Firebase 的 Android 应用程序启动画面

来自分类Dev

退出应用程序时出现启动画面活动

来自分类Dev

红宝石正则表达式不止一次发现模式

来自分类Dev

Terraform:启动一个GKE集群和Ingress应用程序一次

来自分类Dev

C#程序不断询问用户输入问题不止一次

来自分类Dev

Ubuntu 20.04 安装程序不止一次下载 ISO

来自分类Dev

MEF在应用程序启动期间仅实例化一次合成

来自分类Dev

在Android上每个应用程序启动时仅显示一次警报

Related 相关文章

  1. 1

    启动画面显示不止一次

  2. 2

    我在独立应用程序expo-deeplink上进行测试后,调度功能似乎运行了不止一次

  3. 3

    如何使SwingWorker启动不止一次?

  4. 4

    应用程序:didReceiveRemoteNotification:fetchCompletionHandler:调用不止一次。如何避免?

  5. 5

    停止播放动画不止一次

  6. 6

    应用程序停止在启动画面

  7. 7

    CSS样式不止一次应用

  8. 8

    应用程序崩溃后重新启动一次方案

  9. 9

    iOS 8-以横向模式启动应用程序

  10. 10

    创建启动程序以一次启动多个应用程序?

  11. 11

    创建启动程序以一次启动多个应用程序?

  12. 12

    警报返回不止一次

  13. 13

    评论打印不止一次

  14. 14

    从管道读取不止一次

  15. 15

    如何仅显示一次登录,然后直接在android中启动应用程序后显示

  16. 16

    在我的应用程序中制作启动画面后没有操作栏

  17. 17

    Ionic 3 在启动画面后需要很长时间才能进入应用程序

  18. 18

    在ionic 2应用程序中删除启动画面

  19. 19

    OSX Cocoa应用程序中的启动画面

  20. 20

    Android应用程序在Unity启动画面上崩溃

  21. 21

    iOS 移动应用程序中的启动画面(Swift)

  22. 22

    带有 Firebase 的 Android 应用程序启动画面

  23. 23

    退出应用程序时出现启动画面活动

  24. 24

    红宝石正则表达式不止一次发现模式

  25. 25

    Terraform:启动一个GKE集群和Ingress应用程序一次

  26. 26

    C#程序不断询问用户输入问题不止一次

  27. 27

    Ubuntu 20.04 安装程序不止一次下载 ISO

  28. 28

    MEF在应用程序启动期间仅实例化一次合成

  29. 29

    在Android上每个应用程序启动时仅显示一次警报

热门标签

归档