如何过滤特定活动的意图

时间:2013-12-07 05:43:12

标签: android android-intent

MainActivity.java:

    Intent intent = new Intent(MainActivity.this,Profile_view.class);
    intent.putExtra("user",username);
    startActivity(intent);

Profil_view:

    Intent intent = this.getIntent();
    if(intent!=null)
    {
        String user = intent.getExtras().getString("user");
        Log.i("ProfileView",user);
        DBAdapter dbAdapter = new DBAdapter(Profile_view.this);
        dbAdapter.open();
        String name = dbAdapter.getName(user);
        full_name.setText(name);
        username_display.setText(dbAdapter.getUserName(user));
        mail_display.setText(dbAdapter.getMail_id(user));
        date_of_birth_display.setText(dbAdapter.get_date_of_birth(user));
        //country_display.setText(dbAdapter.get_country(user));
        dbAdapter.close();
    }

我有一个情况,我在这里注册用户详细信息并转到profile_view页面显示它,同样当我登录主活动时,它与数据库中的当前用户名匹配并显示他的用户profile_view页面中的详细信息。因此,profile_viewregistration_page都可以main_activity

profile_view中的方法列表应该适用于注册页面和主页 活动页面,但它导致主要活动的意图,它会抛出一个null 从注册页面到达时指针异常。

3 个答案:

答案 0 :(得分:2)

只需输入一个条件来检查意图值,如下所示:

Intent intent = this.getIntent();
    if(intent!=null)
    {
        if(intent.hasExtra("user"))
        {

           //Your Code


         }

     }

答案 1 :(得分:2)

从主要活动中传递意图中的字符串,以确定您来自MainActivity,如下所示:

Intent intent = new Intent(MainActivity.this,Profile_view.class);
intent.putExtra("user",username);
 intent.putExtra("Activity","MainActivity");
startActivity(intent);

在您的个人资料视图活动中,检查以下值:

Intent intent = this.getIntent();
String value= intent.getString("Activity");
if(intent!=null)
{
  if(value.equalsIgnoreCase("MainActivity"))
   {
     String user = intent.getExtras().getString("user");
    Log.i("ProfileView",user);
    DBAdapter dbAdapter = new DBAdapter(Profile_view.this);
    dbAdapter.open();
    String name = dbAdapter.getName(user);
    full_name.setText(name);
    username_display.setText(dbAdapter.getUserName(user));
    mail_display.setText(dbAdapter.getMail_id(user));
    date_of_birth_display.setText(dbAdapter.get_date_of_birth(user));
    //country_display.setText(dbAdapter.get_country(user));
    dbAdapter.close();
   }
     else{
         //Comes from Login activity
        }
  }

答案 2 :(得分:1)

hasExtra()有一个名为Intent属性。您可以利用它来确定是否有额外的可用。

试试这个..

    Intent intent = this.getIntent();
    if(intent!=null)
    {
        if(intent.hasExtra("user"))
        {
             //It means that intent has an extra with tag name - "user".
             //So, it shows that the intent is from MainActivity
             //Do the necessary coding here

             String user = intent.getExtras().getString("user");
             Log.i("ProfileView",user);
             DBAdapter dbAdapter = new DBAdapter(Profile_view.this);
             dbAdapter.open();
             String name = dbAdapter.getName(user);
             full_name.setText(name);
             username_display.setText(dbAdapter.getUserName(user));
             mail_display.setText(dbAdapter.getMail_id(user));
             date_of_birth_display.setText(dbAdapter.get_date_of_birth(user));
             //country_display.setText(dbAdapter.get_country(user));
             dbAdapter.close();
        }
        else
        {
             //It means that intent doesn't have an extra with tag name - "user".
             //So, it shows that the intent is from RegisterActivity
             //Do the necessary coding here
        }
    }

hasExtra("")

的说明