想在我的应用程序中提供合适的导航系统吗?

时间:2018-12-03 14:35:07

标签: android

我想在应用程序中提供适当的反向导航系统。 我列出了应用程序中加载的活动列表。当我按回来时,它将检查列表并加载列表顶部的活动,并从LIST中删除该元素,然后有一个变量名last_id,它跟踪应用程序中加载的最后一个ID,如果在应用程序中加载了相同的活动,但未将其添加到LIST中。 但它不起作用,当我在应用程序中加载相同的活动时,它会记录该活动,并在我按相等次数的次数后退出。例如:-如果我按Main活动上的按钮,该按钮再次加载MainActivity 6次,则应用程序在按退出按钮12次(对于活动2次)后退出。但是我不想要这个,我希望我按下按钮6次,它不会添加到LIST中,并且在按下退出按钮2次(正常退出)后退出。 代码为 主要活动

package com.example.gaurav.phone_get;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    public ImageButton getS;
    other_parts op;
    private Button b;
   static int counter=0;
    protected static final int MAIN_ACT_CODE=102;
    private boolean exit = false;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getS =(ImageButton) findViewById(R.id.getS);
        op = new other_parts(getApplicationContext());
        op.getID(MAIN_ACT_CODE);//calls 1st time otherparts
        b=(Button)findViewById(R.id.Main);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //ActivityInfo vxd = new ActivityInfo();
                //Toast.makeText(MainActivity.this, ""+vxd, Toast.LENGTH_SHORT).show();

                ++counter;
                b.setText(String.valueOf(counter));
                Intent i= new Intent(MainActivity.this,MainActivity.class);
                startActivity(i);

                Log.d("counter ","Increasing the counter ");
            }
        });
    }
    public void getStart(View v)
    {

        load_frag();


    }

    @Override
    public void onBackPressed() {
            if(exit)
            {
                super.onBackPressed();
                finish();
                return;
            }
            else
            {
                if(op.loadback_Activity())
                {
                 this.exit=true;
                 Log.d("if","*****************in the if condition***********");
                }
                else {
                    this.exit = true;
                    Toast.makeText(this, "Press Again to exit", Toast.LENGTH_SHORT).show();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            exit = false;
                        }
                    }, 5000);
                }
            }
    }


    @Override
    protected void onResume() {
        super.onResume();
        op = new other_parts(getApplicationContext());
    }

    @Override
    protected void onPause() {
        super.onPause();
        op = new other_parts(getApplicationContext());
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        op = new other_parts(getApplicationContext());
    }
public void load_frag()
{
    getS.setVisibility(View.GONE);
    counter_class counterClass = new counter_class();
    FragmentManager fragmentManager = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.replace(R.id.frame1, counterClass);
    ft.addToBackStack(null);
    ft.commit();

}
}

counter_class.java的代码

打包com.example.gaurav.phone_get;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class counter_class extends Fragment {
    protected static  final int COUNTER_CLASS_CODE=104;
    Button b;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v;
        other_parts other_parts= new other_parts(getContext());
        v= inflater.inflate(R.layout.counter_layout,container,false);
        other_parts.getID(counter_class.COUNTER_CLASS_CODE);

        return v;
    }
}

other_parts.java的代码

打包com.example.gaurav.phone_get;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;



public class other_parts {
    Context ctx;
    static   boolean status =false;
    other_parts(Context ctx)
    {
        this.ctx= ctx;
    }
    private static int id=0;
    static int i=0;
    public static int last_id=0;
    public static List<Integer>curr_act_code =new ArrayList<Integer>();
       public void getID(int id)
       {
       this.id=id;
       push_in_current(id);

       }
                                            public void push_in_current(int q)
                                            {
                                                        if(curr_act_code.isEmpty()) {
                                                            Log.d("Empty ","Empty string");
                                                            if (last_id == q) {
                                                                Log.d("return this", "Returning back");

                                                            } else {
                                                                curr_act_code.add(q);
                                                                last_id = q;
                                                                Log.d("Adding","Adding first time");
                                                            }

                                                        }
                                                        else
                                                        {
                                                            int x= curr_act_code.get(curr_act_code.size()-1);
                                                            last_id=x;
                                                            if(last_id==id)
                                                            {
                                                                Log.d("2nd time","..........Returning second time ...........");

                                                            }
                                                            else
                                                            {
                                                                curr_act_code.add(q);
                                                                last_id=q;
                                                                Log.d("Adding ", "Adding second time");
                                                                Log.d("current_code_value","current_code_value"+String.valueOf(curr_act_code));
                                                                Log.d("lst_id","Last id value"+String.valueOf(last_id));
                                                            }

                                                        }
                                                        Log.d("cuurent's code","..............."+String.valueOf(curr_act_code)+"...........");
                                            }
public boolean loadback_Activity(){

       if(!curr_act_code.isEmpty())
       {
           Log.d("Before deletion", String.valueOf(curr_act_code));
           status = true;
           int x = curr_act_code.get(curr_act_code.size() - 1);//finds values
           int k = curr_act_code.size()-1;//finds index
           curr_act_code.remove(k);
           Log.d("After deletion",String.valueOf(curr_act_code));
           if(curr_act_code.isEmpty()) {
               status=false;
           }
           else
           {
               Log.d("search activity","Initating the search");
               search_activity(x);
           }


       }
       else
       {
           status =false;
           Log.d("false","in the last else");
       }
       return status;
}
public void search_activity(int x)
{
    Log.d("Search ","in the Search");
    if(x==102)
    {
        Intent i= new Intent(ctx,MainActivity.class);
        ctx.startActivity(i);
    }
    else if(x ==104)
    {
        MainActivity m = new MainActivity();
        m.load_frag();
    }


}
}

XML文件是 :-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <FrameLayout android:id="@+id/frame1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <ImageButton
        android:layout_width="64dp"
        android:layout_height="66dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:onClick="getStart"
        android:id="@+id/getS"
        android:layout_marginStart="169dp"
        android:layout_marginTop="180dp"
        android:background="@drawable/round_button"
        android:src="@drawable/counter" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Main"/>
</RelativeLayout>

任何人都可以帮我吗.....

1 个答案:

答案 0 :(得分:0)

要在android应用中提供适当的导航机制,您无需自己编写代码,因为android metric_a保留在Stack中,而您只需要实现Activities和{{1} },以使该功能正常运行。

请在此处关注正式文件:https://developer.android.com/training/appbar/

但是在开始实现ActionBar之前,请先了解 BackStack 以及android如何在此处进行维护:https://developer.android.com/guide/components/activities/tasks-and-back-stack